0

I am trying to retrieve the body of ContentDocument after insert. Below is my code.My trigger is on ContentDocument (After Insert)

 public static void syncContentDocument(List<ContentDocument> documentList) {

        for (ContentDocument attach : documentList) {
            contentIds.add(attach.id);
        }
        //create content document body map
        for (ContentVersion content : [SELECT Id,Title,VersionData FROM ContentVersion WHERE ContentDocumentId IN : contentIds AND IsLatest = true]) {
            bodyContent.put(content.Id, content.VersionData);
        }
        System.debug('contentIds' + contentIds);
        System.debug('bodyContent' + bodyContent);

}

But I am getting empty result from debug logs. Could anyone please help me to do this?

1

Speaking across Salesforce, child objects are not available for query during the insert event for their parents. ContentDocument is the parent object for one or many ContentVersion records; you will not be able to query those ContentVersion objects during the after insert event for their parent ContentDocuments. Likewise, you won't be able to access Contact records parented under an Account during the insert event of the `Account.

Instead, consider writing a trigger against ContentVersion insert events, when you will be able to access these records and their contained data.

  • Hi David.. As you told I have tried the trigger on ContentVersion. I can able to get the records of other fields like( title,id) BUT ContentVersion is still empty. – Thirumagal.s Feb 27 at 4:06
  • public static void syncContentDocument(List<ContentVersion> documentList) { for (ContentVersion attach : documentList) { bodyContent.put(attach.id, attach.VersionData); } System.debug('bodyContent' + bodyContent); } here Id is coming But ContentVersion is empty – Thirumagal.s Feb 27 at 4:12
  • Hi Finally I got the versionData like this VersionData=Blob[100730] But how can I get the file body from this? – Thirumagal.s Feb 27 at 5:43
  • @jury I think you should raise a separate question or you may search for other question on how to convert blob to a string value. – sanket kumar Feb 27 at 6:01
  • Okay. Thank You !! – Thirumagal.s Feb 27 at 8:51

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.