Find your content:

Search form

You are here

Update LastModifiedDate field of custom object

 
Share

I'm uploading attachments to a custom object using the following JS. I want to update the LastModifiedDate field of the custom object each time I do this. Would anyone have any suggestions as to how I might accomplish this?

My initial attempt in the onSuccess function below does not work...

function uploadFile(customObj)
{       
var input = document.getElementById('file-input');
var parentId = customObj.Id

var filesToUpload = input.files;

for(var i = 0, f; f = filesToUpload[i]; i++)
{
    var reader = new FileReader();     

    // Keep a reference to the File in the FileReader so it can be accessed in callbacks
    reader.file = f; 

    reader.onerror = function(e) 
    {
        switch(e.target.error.code) 
        {
            case e.target.error.NOT_FOUND_ERR:
                alert('File Not Found!');
                break;
            case e.target.error.NOT_READABLE_ERR:
                alert('File is not readable');
                break;
            case e.target.error.ABORT_ERR:
                break; // noop
            default:
                alert('An error occurred reading this file.');
        };
    };     

    reader.onabort = function(e) 
    {
        alert('File read cancelled');
    };

    reader.onload = function(e) 
    {
        var att = new sforce.SObject("Attachment");
        att.Name = this.file.name;
        att.ContentType = this.file.type;
        att.ParentId = parentId;

        att.Body = (new sforce.Base64Binary(e.target.result)).toString();

        sforce.connection.create([att],
        {
            onSuccess : function(result, source) 
            {
                if (result[0].getBoolean("success")) 
                {
                  var test_date = customObj;              <-- POSSIBLE SOLUTION?
                  test_date.LastModifiedDate = new Date();<-- POSSIBLE SOLUTION?                     
                  sforce.connection.update([test_date ]); <-- POSSIBLE SOLUTION?  

                   console.log("new attachment created with id " + result[0].id);
                } 
                else 
                {
                    console.log("failed to create attachment " + result[0]);
                }
            }, 
            onFailure : function(error, source) 
            {
                console.log("An error has occurred " + error);
            }
        });
    };

    reader.readAsBinaryString(f);
}
}

Attribution to: Daft

Possible Suggestion/Solution #1

Just "touch" the record with an update that doesn't change anything. Similar to how user would hit Edit and then Save without changing anything on the page.

This will cause all workflows, validation rules etc to run of course!

Something like this should be enough:

var parent = new sforce.SObject("Account");
parent.id = customObj.Id;
result = sforce.connection.update([parent]);

(even better if the object you're passing to the function already is created with this library and not some totally separate data structure you've made that has pretty much only the id attribute)

Having said that... maybe you should have this bit of logic in a trigger and not Javascript? Should it work only for these uploads or anytime an attachment is added to your custom object?


Attribution to: eyescream
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/34159

My Block Status

My Block Content