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
Salesforce seekarSalesforce seekar 

Trigger help needed on content document.... parent to child rel

HI All ,

when i upload a file on Account i need a trigger to upload the same file to the cases under the acount... any sample code or help or ideas needed
Best Answer chosen by Salesforce seekar
AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Try with below code.
trigger Contentdcupdate on ContentDocumentLink (After insert) {
    
    set<id> accids = new set<id>();    
    for(ContentDocumentLink cdk : trigger.new){
        
        accids.add(cdk.LinkedEntityId);
        
    }
    
   map<id,case> caselistupdate = new map<id,case>([select id,Accountid from case where Accountid=:accids]);
    
    List<ContentDocumentLink> cdllist = new List<ContentDocumentLink>();
    
    for(ContentDocumentLink cdk : trigger.new){
        for(case cs:caselistupdate.values()){
            
            if(cdk.LinkedEntityId ==cs.accountid){
                ContentDocumentLink cd = new ContentDocumentLink();
                
                cd.LinkedEntityId = cs.id;
                cd.ContentDocumentId = cdk.ContentDocumentId;
                cd.ShareType = cdk.ShareType;
                cd.Visibility = cdk.Visibility;
                cdllist.add(cd);
                
                
            }
        }
    }
    
    if(cdllist.size()>0){
        
        insert cdllist;
    }
    
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi,

Try with below code.
trigger Contentdcupdate on ContentDocumentLink (After insert) {
    
    set<id> accids = new set<id>();    
    for(ContentDocumentLink cdk : trigger.new){
        
        accids.add(cdk.LinkedEntityId);
        
    }
    
   map<id,case> caselistupdate = new map<id,case>([select id,Accountid from case where Accountid=:accids]);
    
    List<ContentDocumentLink> cdllist = new List<ContentDocumentLink>();
    
    for(ContentDocumentLink cdk : trigger.new){
        for(case cs:caselistupdate.values()){
            
            if(cdk.LinkedEntityId ==cs.accountid){
                ContentDocumentLink cd = new ContentDocumentLink();
                
                cd.LinkedEntityId = cs.id;
                cd.ContentDocumentId = cdk.ContentDocumentId;
                cd.ShareType = cdk.ShareType;
                cd.Visibility = cdk.Visibility;
                cdllist.add(cd);
                
                
            }
        }
    }
    
    if(cdllist.size()>0){
        
        insert cdllist;
    }
    
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
This was selected as the best answer
Salesforce seekarSalesforce seekar
Thanks ankaih