+ Start a Discussion
Bhanu yadav 26Bhanu yadav 26 

Whenever a new contact is created under any account. Copy all the attachment of account to its child contact.

Deepali Jain 25Deepali Jain 25
Hi Bhanu,

trigger CopyAttachment on Contact (after insert) {
    Set<Id> accIdSet = new Set<Id>();
    for(Contact con : trigger.new) {
        if(con.AccountId != NULL) {
            accIdSet.add(con.AccountId);
        }
    }
    if(!accIdSet.isEmpty()) {
        Map<Id, Set<Id>> documentMap = new Map<Id, Set<Id>>();
        for(ContentDocumentLink cdl : [SELECT LinkedEntityId, ContentDocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN :accIdSet]) {
            if(!documentMap.containsKey(cdl.LinkedEntityId)) {
                documentMap.put(cdl.LinkedEntityId, new Set<Id>{cdl.ContentDocumentId});
            }
            else {
                documentMap.get(cdl.LinkedEntityId).add(cdl.ContentDocumentId);
            }
        }
        List<ContentDocumentLink> updateList = new List<ContentDocumentLink>();
        for(Contact con : trigger.new) {
            if((con.AccountId != NULL) && (documentMap.containsKey(con.AccountId))) {
                for(Id idObj : documentMap.get(con.AccountId)) {
                    ContentDocumentLink cdlObj = new ContentDocumentLink();
                    cdlObj.ContentDocumentId = idObj;
                    cdlObj.LinkedEntityId = con.Id;
                    updateList.add(cdlObj);
                }
            }
        }
        INSERT updateList;
    }
}

Thank you!
Lukesh KarmoreLukesh Karmore
Hello Bhanu, 
Mark the best answer if it solve your issue.


trigger WhenContactIsCreatedUnderAccount_CopyAllFilesOfAccountUnderContact on Contact (After insert) {
set<id> acctIds=new set<id>();
for(Contact con:Trigger.new){
    If(con.AccountId!=null){
        acctIds.add(con.AccountId);
    }
}
map<Id,set<id>> documentIds=new map<Id,set<id>>();
for(ContentDocumentLink cdl:[SELECT LinkedEntityId,ContentdocumentId FROM ContentDocumentLink WHERE LinkedEntityId IN:acctIds]){
    if(!documentIds.containsKey(cdl.LinkedEntityId)){
        documentIds.put(cdl.LinkedEntityId,new set<id>{cdl.ContentdocumentId});
    }
    else{
        documentIds.get(cdl.LinkedEntityId).add(cdl.ContentdocumentId);
    }
}
list<ContentDocumentLink> Updateddoc=new list<ContentDocumentLink>();
for(Contact con:Trigger.new){
    If((con.AccountId!=null) && (documentIds.containsKey(con.AccountId))){
       for(Id Ids:documentIds.get(con.AccountId)){
        ContentDocumentLink cd=new ContentDocumentLink();
        cd.LinkedEntityId=con.Id;
        cd.ContentdocumentId=Ids;
        Updateddoc.add(cd);
       }
    }
}
Insert Updateddoc;
}
Thank you
Suraj Tripathi 47Suraj Tripathi 47
Hi Bhanu,
please find the below solution
 
 
trigger triggerOnContactNEW on Contact (after insert, after update) {
    set <Id> accountIdCon =new set<Id>();
    
    list<contact> contactListObj=[select Id, LastName,accountId from contact where accountId!=null and id in:trigger.new limit 1000];
    for(contact con:contactListObj){
        accountIdCon.add(con.accountId);
    }
    system.debug(contactListObj);
    system.debug(accountIdCon);
    list<Attachment> list_obj_Attachment1=[select id, parentId, body, name from attachment where parentId IN:accountIdCon limit 1000];
    system.debug(list_obj_Attachment1);
    list<Attachment> list_obj_Attachment2=new list<Attachment>();
    
    for(Attachment attch:list_obj_Attachment1){
        for(contact con:contactListObj){
            if(con.accountId == attch.parentId){
                
                     Attachment obj_attch=new Attachment();
                    obj_attch.Name=attch.Name;
                    obj_attch.Body=attch.body;
                    obj_attch.ParentId=con.Id;
                    list_obj_Attachment2.add(obj_attch);
                
            }
        }
    }
            system.debug('Update Successfully');
            system.debug(list_obj_Attachment2);
            upsert list_obj_Attachment2;
   
}


"If you are using the Salesforce Classic then upload the attachment file in Salesforce Classic and then try this code, it's working perfectly in Salesforce Classic Or if you are using Salesforce Lighting then Please Use ContentDocumentLink as sObject name instead of Attachment sObject Name in Above Code"
"Want to take any help please comment."


If you find your Solution then mark this as the best answer. 


Thanks & Regards,
Suraj Tripathi