Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

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);
}
}
share|improve this question

1 Answer 1

up vote 3 down vote accepted

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?

share|improve this answer
    
Thanks for the response eyescream, and I think you're right, it should work whenever someone adds an attachment and not just through this particular script... –  Daft May 1 at 10:59
    
But at the same time, I would really like to get this approach to work. At the moment I'm getting the following error when using your approach: An error has occurred {faultcode:'sf:INVALID_TYPE', faultstring:'INVALID_TYPE: sObject type 'Answer__c' is not supported. If you are attempting to use a custom object, be sure to append the '__c' after the entity name. Answer__c is my custom object, should I handle this differently to a standard object? –  Daft May 1 at 11:09
    
Should work the same. Might be something to do with Profile. Do you have read & edit rights to the object and is the tab not marked as hidden? Is it really your object or is it a part of managed package (needs to be namespace__Answer__c in that case)? –  eyescream May 1 at 11:26
    
I don't think it's a read/write issue, and I have some code which attaches the correct prefix... very frustrating. –  Daft May 1 at 11:37
1  
And suddenly it's working! Maybe there was a typo I corrected somewhere while working on you're suggestion?? Anyway, you're idea worked! Much appreciated! –  Daft May 1 at 12:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.