1

How to retrieve AccountId on insert of file.

system.debug('LinkedEntityId $$$$'+LinkedEntityId)

always gives OwnerId.

But after insert of file, I can retrieve two rows from below query on ContentDocumentLink - one row with AccountId(LinkedEntityId) and one row with OwnerID(LinkedEntityId)

SELECT LinkedEntityId, Id, ContentDocumentId FROM ContentDocumentLink where ContentDocumentId ='069r00000002COX'

trigger UpdateAccountWithFileLink on ContentDocumentLink (after insert) {
        list<Id> accIdList = new list<Id>();
        for (ContentDocumentLink cdl : trigger.new) {
            system.debug('LinkedEntityId $$$$'+LinkedEntityId);
            if(String.valueOf(cdl.LinkedEntityId).startswith('001')){
              accIdList =  cdl.LinkedEntityId; 
            }
        }
    }
5
  • if your event is INSERT of a file why don't you create a trigger on ContentDocument instead of ContentDocumentLink?
    – Andrei.Z
    Oct 6, 2016 at 6:47
  • @Andrei.Z ParentID is not available on ContentDocument
    – sfdc
    Oct 6, 2016 at 13:50
  • What's the problem here? Are you not getting the accountid is from linkedentityid?
    – Preya
    Oct 6, 2016 at 13:56
  • am not getting accountid from linkedentityid.. But after insert of file, I can retrieve two rows from below query on ContentDocumentLink - one row with AccountId(LinkedEntityId) and one row with OwnerID(LinkedEntityId)
    – sfdc
    Oct 6, 2016 at 14:02
  • SELECT LinkedEntityId, Id, ContentDocumentId FROM ContentDocumentLink where ContentDocumentId ='069r00000002COX'
    – sfdc
    Oct 6, 2016 at 14:03

2 Answers 2

0

@Andrei.Z ParentId is available on ContentDocument if you enable CRM Content and assign to users.

1
  • 1
    Welcome to SF.SE Peter. For ContentDocument, the ParentId is the ID of the library that owns the document. It's created automatically when inserting a ContentVersion via the API for the first time. I don't think that's the same ParentId that he's looking for. Its best to provide a reference for your answers when posting on SF.SE.
    – crmprogdev
    Dec 10, 2016 at 16:19
0

If you're needing the ContentDocumentLink record within the trigger context i.e. at the time of the record insertion rather than at any time afterwards, can't you just filter out the record where LinkedEntityId = :UserInfo.getUserId()?

SELECT ContentDocumentId,Id,IsDeleted,LinkedEntityId,ShareType,SystemModstamp,Visibility FROM ContentDocumentLink WHERE ContentDocumentId = '0698d000009mNclAAE' AND LinkedEntityId != :UserInfo.getUserId()

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .