Salesforce的sharing Rule 不支持Lookup型字段解决方案

时间:2023-03-09 00:41:49
Salesforce的sharing Rule 不支持Lookup型字段解决方案

Salesforce 中 sharing rule 并不支持Look up 字段 和 formula 字段。但在实际项目中,有时会需要在sharing rule中直接取Look up型字段的值,解决方案就是在目标object上新建一个字段,写trigger,将需要获取的Lookup 字段的value 灌入新字段中(我现在就要取Product Catalog表中的External ID字段的值,当USER每次在 Clm_Presentation_vod__c 表 上insert 或者 update 数据时,就将Product_vod__c.External_ID_vod__c的值update到新建的External_ID_vod__c 字段上)。对应代码:

Salesforce的sharing Rule 不支持Lookup型字段解决方案

对应代码:

trigger NOV_Clm_Presentation_Before_InsertUpdate on Clm_Presentation_vod__c(before insert, before update){
set<Id> productIdSet=new set<Id>();
system.debug('-----------------------'+productIdSet);
for(Clm_Presentation_vod__c presentation:trigger.new){
if(presentation.Product_vod__c!=null){
productIdSet.add(presentation.Product_vod__c);
system.debug('-----------------------'+productIdSet);
}
}
map<id,Product_vod__c> productMap=new map<id,Product_vod__c>([select id,External_ID_vod__c
from Product_vod__c
where id in:productIdSet
]);
system.debug('-----------------------'+productMap);
for(Clm_Presentation_vod__c presentation:trigger.new){
if(productMap.containsKey(presentation.Product_vod__c)){
presentation.External_ID_vod__c=productMap.get(presentation.Product_vod__c).External_ID_vod__c;
system.debug('-----------------------'+presentation.External_ID_vod__c);
}
}
}