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

Copy Attachment from Contact record to Account record

I want the ability to copy a file attachment when inserted into the contact record, be copied over to the Account record. Here is the Apex code and have inserted into my Dev Sandbox but nothing happens when I insert an attachment into the contact record. I receive no errors either. Any help on this would be great!

trigger CopyContactAttachmentAccounts on Attachment (after insert) {
    Set<ID> attId = new Set<ID>();
    Set<ID> contactID = new Set<ID>();
    /* ******This block collects all the attachment IDs*****************/
    for(Attachment att : trigger.New){
         //Check if added attachment is related to Contact or not
         if(att.ParentId.getSobjectType() == Contact.SobjectType){
    /*******************Below block creates the attachment on Account***************/
    list<attachment> accatt=new list<attachment>();
    List<Attachment> attachmentList = [SELECT Name, Id, ParentID,Body FROM Attachment WHERE ID IN :attID];
    Map<Id, Contact> contactDetailMap = new Map<Id, Contact>([Select Id,, from Contact where Id IN :contactID]);
    for(Attachment att: attachmentList) {
        Attachment newFile = new Attachment(Name =, body = 
        att.body,ParentId= contactDetailMap.get(att.ParentID).Accountid);
     insert accatt;


PriyaPriya (Salesforce Developers) 


Can you put some debug statement and check if the variables like attachmentList, contactDetailMap, etc have values into it or not.


Priya Ranjan


Here's the output: 


User-added image