salesforce - 字段更改时将附件从父对象复制到子对象
问题描述
尝试在机会(父对象)上插入附件时将附件从父对象复制到子对象
我试过写一些代码。
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 时……它对我不起作用!
解决方案
您ruMap
的密钥由EIP_Lead_Rental_Object__c
id 制成。但是您尝试get()
使用 Opportunity Id 调用它。这永远不会奏效。我很惊讶它没有给你抛出一个与 null 相关的错误,你那里有一些 try-catch 可以吞下异常吗?
你可能需要类似的东西
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);
}
}
推荐阅读
- python - 将 2 个向量的元素转换为矩阵的 Python 乘积
- java - 如何在开发 Harmony 应用程序时将标签内的图像添加为图标?
- unity3d - Unity 2d 中是否有任何功能,当我死时游戏结束屏幕只有在死亡动画完成后才会出现
- reactjs - FIrebase Google Auth 注销错误连接被拒绝
- python-3.x - numpy-1.19.5 不是此平台上支持的轮子,(RHEL 7.9 64 位)
- excel - 在工作表中插入缺少的列并将数据粘贴到新的 Excel 中
- html - 如何使用css在标题下放置线条和点
- android-studio - 插件错误:插件“Visual Paradigm SDE for IntelliJ IDEA(社区版)”不兼容(仅在 IntelliJ IDEA 中支持)
- c++ - 我可以将矩形范围转换为增强多边形吗?
- apache-superset - Apache Superset 仪表板中有两种选项卡。如何制作适用于整个仪表板的那种?