0

I am trying to share a file with a grandchild record. Parent: Account: Child of Account: Contact Child of Contact: Invoice

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];

Not sure how to get the LinkedEntityId assigned to the Invoice record

P

1 Answer 1

0

We can make this simpler by leveraging the ability to query from grandchild to grandparent

// Get CDLs related to Accounts (note use of Account.SObjectType)
Map<Id,ContentDocumentLink[]> cdlsByAccountId = new Map<Id,ContentDocumentLink[]>();
for(ContentDocumentLink cdl : (List<ContentDocumentLink>)Trigger.new){
   if(cdl.LinkedEntityId.getSObjectType() == Account.SObjectType) {              
      if (!cdlsByAccountId.containsKey(cdl.LinkedEntityId)) {
          cdlsByAccountId.put(cdl.LinkedEntityId,new List<ContentDocumentLink>());
      }
      cdlsByAccountID.get(cdl.LinkedEntityId).add(cdl); // add to list of cdls by acctId
   }
}

// Get all Invoices whose grandparent AccountId matches
ContentDocumentLink[] invoiceCdls = new List<ContentDocumentLink>();
for (Invoice i : [SELECT Id, Contact__r.AccountId 
                    FROM Invoice
                    WHERE Contact__r.AccountId IN :cdlsByAccountId.keySet() AND
                          Category__c = 'Date Received']) {
    for (ContentDocumentLink cdl: cdlsByAccountId.get(i.Contact__r.AccountId)) {
      ContentDocumentLink invoiceCdl = cdl.clone(); // clone the CDL to the Account
      invoiceCdl.LinkedEntityId = i.Id;             // point the clone to the Invoice
      invoiceCdls.add(invoiceCdl);                  // add to list of invoiceCDls to insert
    }
}

insert invoiceCdls;

You must log in to answer this question.

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