尝试在机会(父对象)上插入附件时将附件从父对象复制到子对象
我试过写一些代码。
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;
}
}
每次附件都附加到 Opportunity 时……它对我不起作用!