Trying copying Attachments from parent object to child object when attachment gets inserted on Opportunity(Parent Obj)
I have tried writing some code.
trigger CopyAttachmentsToRU on Attachment (after insert) {
Set<Id> OppIds = new Set<Id>();
for(Attachment file : Trigger.new) {
// only collect those that are for the Opportunity object (others can be ignored)
if(file.ParentId.getSObjectType() == Opportunity.getSObjectType()) {
OppIds.add(file.ParentId);
system.debug(OppIds);
}
}
if(!OppIds.isEmpty()) {
Map<Id,EIP_Lead_Rental_Object__c> ruMap = new Map<Id,EIP_Lead_Rental_Object__c>([select EIP_Opportunity__c from EIP_Lead_Rental_Object__c where EIP_Opportunity__c in : OppIds]);
List<Attachment> attachments = new List<Attachment>();
system.debug(ruMap);
for(Attachment file : Trigger.new) {
Attachment newFile = file.clone();
newFile.ParentId = ruMap.get(file.ParentId).Id;
attachments.add(newFile);
}
// finally, insert the cloned attachments
insert attachments;
}
}
Every time an attachment gets attach to Opportunity..its not working for me !