Salesforce检查某字段前后是否被更改的方法

来源:互联网 发布:surge mac怎么用 编辑:程序博客网 时间:2024/06/06 16:55

场景描述:开发中经常遇到记录一旦创建,其中的某个字段就不允许被修改的需求,以保证记录的安全性,以下提供两种解决方案。

方案一:使用Validation Rules实现
Sample:当记录第一次被保存后,就不允许用户修改其中某个字段-isCheck:


Validation Rule:


最终效果:


方案二:使用Trigger实现

Sample:比较old和new fields是否相同:oldMap适用于Before Update

for(Opportunity opp : Trigger.new){//Create an old and new map so that we can compare valuesOpportunity oldOpp = Trigger.oldMap.get(opp.ID);    Opportunity newOpp = Trigger.newMap.get(opp.ID);//Retrieve the old and new Reseller Email Field            string oldResellerEmail = oldOpp.Reseller_Email__c;string newResellerEmail = newOpp.Reseller_Email__c;//If the fields are different, the email has changedif(oldResellerEmail != newResellerEmail){    oppIDs.put(opp.Id,opp.Reseller_Email__c);    }}


原创粉丝点击