0

Need to update the body of the file before uploading into salesforce. Here is the sample code i am using it for updating attachment

trigger SetTitleToAttachment on Attachment(before insert) {
    for (Attachment att: Trigger.new) {
        if (att.ContentType == 'text/plain') {
            String s = att.body.toString();
            att.body = blob.valueOf('Text Encryption\n' + s);
        }
    }
}
2
  • Yes , I have to add some custom test.I have been using this for uploading attchment. the same thing i am trying to implement it while uploading files....Here is the code i am using it for attachment. Aug 18 '16 at 7:16
  • have been trying this but couldn't get, please share if you have any sample code to for the same or give some idea to achieve this... Aug 18 '16 at 7:29
0

I have worked on this and finally got the solution(trigger) which will help us to update the file before uploading. We can update title,body etc....

trigger setcontentdoclink on ContentDocumentLink(before insert) {

    ContentVersion cont = new ContentVersion();
    ContentVersion cont1 = new ContentVersion();



    for (ContentDocumentLink cd: Trigger.new) {

        if (cd.ContentDocumentId != null) {
            cont.ContentDocumentId = cd.ContentDocumentId;
        }
        cont1 = [SELECT VersionData FROM ContentVersion where ContentDocumentId =: cd.ContentDocumentId limit 1];


    }
    cont.PathOnClient = 'file_' + Datetime.now().getTime() + '.txt';
    string docbody = 'Eventdex Enchryption\n' + cont1.VersionData.toString();
    cont.VersionData = Blob.valueOf(docbody);
    cont.Origin = 'H';
    insert cont;

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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