1

I believe this question is a possible continuation of this other question:

How to retrieve the file content for a ContentDocument object?

I am trying to get the body of a JSON file that is uploaded in lightning, in classic this was working fine using the trigger:

trigger ClassicAttachment on Attachment (before insert, before update, after insert, after update, after delete) {
    for (Attachment a:trigger.new) {
        a.Body 
        .....
    }

Now in lightning i am struggling to understand how can I get the body, I can see that the new file is a combination of records in the backend, but based on that answer, do I need to do a query to get this body? Is there a way to avoid doing the query and getting the actual content of the file in a similar way to classic attachments?

I have tried so far:

trigger LightningAttachment on ContentVersion (before insert) {

trigger LightningAttachment_v2 on ContentDocument (before insert) {

trigger LightningAttachment_v3 on ContentDocumentLink (before insert) {

But I cant find in there the actual body

2

ContentVersion works very different from Attachment Object.

When you have a trigger on it for before insert, You will get these value in trigger.new:-

ContentVersion:{Id=null, ContentDocumentId=null, IsLatest=false, ContentUrl=null, ContentBodyId=05TB000000AUAAuMAP, VersionNumber=null, Title=Capture, Description=null, ReasonForChange=null, SharingOption=A, SharingPrivacy=N, PathOnClient=Capture.PNG, RatingCount=0, IsDeleted=false, ContentModifiedDate=null, ContentModifiedById=null, PositiveRatingCount=0, NegativeRatingCount=0, FeaturedContentBoost=null,
FeaturedContentDate=null, OwnerId=005B0000005htWrIAI, CreatedById=005B0000005htWrIAI, CreatedDate=2019-07-06 03:49:08, LastModifiedById=null, LastModifiedDate=null, SystemModstamp=null, TagCsv=null, FileType=null, PublishStatus=U, VersionData=null, ContentSize=0, FileExtension=null, 
FirstPublishLocationId=005B0000005htWrIAI, Origin=C, NetworkId=null, ContentLocation=S, TextPreview=null, ExternalDocumentInfo1=null, ExternalDocumentInfo2=null, ExternalDataSourceId=null, Checksum=null, IsMajorVersion=false, IsAssetEnabled=false}

On After Insert, You Will get these values:-

DEBUG|ContentVersion:{Id=068B0000005kZ0cIAE, ContentDocumentId=069B0000005gBgSIAU, IsLatest=true, ContentUrl=null, ContentBodyId=05TB000000AUAAzMAP, VersionNumber=1, Title=download, Description=null, ReasonForChange=null, SharingOption=A, SharingPrivacy=N, PathOnClient=download.jpg, RatingCount=0, IsDeleted=false,
 ContentModifiedDate=2019-07-06 03:52:14, ContentModifiedById=005B0000005htWrIAI, PositiveRatingCount=0, NegativeRatingCount=0, FeaturedContentBoost=null, FeaturedContentDate=null, OwnerId=005B0000005htWrIAI, CreatedById=005B0000005htWrIAI, CreatedDate=2019-07-06 03:52:14, LastModifiedById=005B0000005htWrIAI,
 LastModifiedDate=2019-07-06 03:52:14, SystemModstamp=2019-07-06 03:52:14, TagCsv=null, FileType=JPG, PublishStatus=R, VersionData=null, ContentSize=8301, FileExtension=null, FirstPublishLocationId=005B0000005htWrIAI, Origin=C, NetworkId=null, ContentLocation=S,
 TextPreview=null, ExternalDocumentInfo1=null, ExternalDocumentInfo2=null, ExternalDataSourceId=null, Checksum=null, IsMajorVersion=false, IsAssetEnabled=false}

In both the case, VersionData where the body of the file gets stored are null. You have to do an extra query to get the VersionData.

Note:- ContentDocument and ContentDcumentLink does not have versionData Field. So no use of writing a trigger on them.

Also, The only value that gets updated in the Apex instance for any SObject when you insert is the Id value. Other fields populated as part of the insert by triggers or other logic have to be re-queried to get the values - ContentVersion is no different.

Refer my this question:- ContentDocumentID is not available just after ContentVersion getting inserted But available after soql query

| improve this answer | |
  • I understand tehy work very different, but perhaps i didnt explain very clear the question, I need to grab the body of the attachment in order to read it and process it, ... The query that you mention is to query the same object that initialize the trigger? as that is what is in the link provided and that is what I am confused as that doesnt have sense, for instance if the file uploaded is a JSON and you need to read this JSON, how would you ammend the trigger, trigger LightningAttachment on ContentVersion (after insert) { – manza Jul 6 '19 at 6:43

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.