You need to sign in to do that
Don't have an account?
kjun
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){ attId.add(att.Id); contactID.add(att.ParentId); } } /*******************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, Account.id,Account.name from Contact where Id IN :contactID]); for(Attachment att: attachmentList) { Attachment newFile = new Attachment(Name = att.name, body = att.body,ParentId= contactDetailMap.get(att.ParentID).Accountid); accatt.add(newFile); } if(accatt.size()>0){ insert accatt; } }
Hey
Can you put some debug statement and check if the variables like attachmentList, contactDetailMap, etc have values into it or not.
Regards,
Priya Ranjan
Here's the output: