function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Phuc 2Phuc 2 

Sharing Content document link with grandchild

Hello,
Trying to share a file with the grandchild record.
How can I share the contentdocumentlionk?

So file is uploaded to account.
Acount has multiple Contacts.
Contacts have multiple Invoices.
Need to share the file with Invoice records.

Thank you,
P
SwethaSwetha (Salesforce Developers) 
HI Phuc,
What happens when you give the LinkedEntityId as Invoice record Id? 

Something like SELECT ContentDocument.title FROM ContentDocumentLink WHERE ContentDocumentId = '069D00000000so2' AND LinkedEntityId = '0D5000000089123'

Related: https://salesforce.stackexchange.com/questions/323500/contentdocumentlink-sharing

Thanks
Phuc 2Phuc 2
Hello Swetha,
This is what I have so far:
if(Trigger.isInsert){
        Set<Id> getAccts = new Set<Id>();
        Map<String,String> m_accts = new Map<String,String>();

        for(ContentDocumentLink contDocLink : (List<ContentDocumentLink>)Trigger.new){
            if(contDocLink.LinkedEntityId.getSObjectType().getDescribe().getName() == 'Account'){
                if(!getAccts .contains(contDocLink.LinkedEntityId)){
                    getAccts .add(contDocLink.LinkedEntityId);
                }
            }
        }
      

        if(!getAccts .isEmpty()){
            Map<Id, Contact> m_accts= new Map<Id, Contact>([
                SELECT Id, ,
                (SELECT Id, Account, category__c FROM Invoices__r  WHERE category__c = 'Date Received')
                FROM Contact WHERE Account IN: getAccts
            ]);

            system.debug('m_accts' +m_accts);

            List<ContentDocumentLink> linkList = [ SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN: getAccts  ];
            system.debug('linkList ' +linkList);

            if( !linkList.isEmpty() ){
                for( ContentDocumentLink link : linkList ){
                    List<ContentDocumentLink> links = this.linkMap.containsKey( link.ContentDocumentId )
                        ? this.linkMap.get( link.ContentDocumentId ) : new List<ContentDocumentLink>();
                    links.add( link );
                    this.linkMap.put( link.ContentDocumentId, links );
                }
            }
            system.debug('linkList ' +linkList);

List<ContentDocumentLink> linkList = [ SELECT Id, ContentDocumentId, LinkedEntityId FROM ContentDocumentLink WHERE ContentDocumentId IN: getAccts];

Just not sure how to get the LinkedEntityId  assigned to the invoice record.  Any suggestions?​​
SwethaSwetha (Salesforce Developers) 
I see you also posted on SSE. Does the answer posted on https://salesforce.stackexchange.com/questions/390192/share-linkedentityid-with-grandchild/390216#390216 address your scenario?

Thanks