尝试在机会(父对象)上插入附件时尝试将附件从父对象复制到子对象
我尝试编写一些代码。
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;
}
}
每次附件都附有机会。.它对我不起作用!
答案 0 :(得分:1)
您的ruMap
具有由EIP_Lead_Rental_Object__c
id组成的密钥。但是,您尝试使用商机ID对其调用get()
。这将永远无法工作。我很惊讶它不会引发与null相关的错误,您是否在那里有一些尝试捕获只是吞没了异常?
您可能需要类似的东西
Map<Id,EIP_Lead_Rental_Object__c> ruMap = new Map<Id,EIP_Lead_Rental_Object__c>();
for(EIP_Lead_Rental_Object__c obj : [select Id, EIP_Opportunity__c from EIP_Lead_Rental_Object__c where EIP_Opportunity__c in : OppIds]){
ruMap.put(obj.EIP_Opportunity__c, obj);
}
然后您就可以
for(Attachment file : Trigger.new){
if(ruMap.containsKey(file.ParentId)){
Attachment newFile = file.clone();
newFile.ParentId = ruMap.get(file.ParentId).Id;
attachments.add(newFile);
}
}