我是 Salesforce 的新手,我正在尝试创建一个触发器,该触发器基本上会更新字段并在每次添加新机会时创建一个新的机会所有者。
为清楚起见,我在下面附上了我的代码:
trigger trig_Opportunity_CreateOppOwner on Opportunity (before insert, before update) {
//Opportunity OppOwner = null;
List<id>OppsID = new List<id>(); //Get the id of all new Opportunities owners
for (Opportunity Opp : Trigger.new) { //If a new Opportunity is added, then create new OppOwner, if not, then don't add.
OppsId.add(Opp.ID); //adds all new Opportunities Id's
}
List<Opportunity>OppToUpdate = [SELECT Id,
Name,
Owner__c,
OppOwner,
FROM Opportunity
WHERE Id IN: Opp.ID // Select Id, OpportunityName,
];
if Trigger.oldMap.get(opp.id).Owner__c != Trigger.oldMap.get(OppToUpdate.id).Owner__c // verify that if previous Opportunity has a matching owner.
OppsId.add(Opp.ID); //populates new oppowner with ID's of all owners.
这基本上就是我想要做的:触发器(更新前,插入前){
- 获取所有触发的机会。
- 验证旧机会是否已有匹配的所有者。
- 如果不是匹配的所有者,请更新商机字段并更新商机。
我不确定如何从第 2 步到第 3 步。任何帮助将不胜感激。