Triggers 创建一个触发器,每当Salesforce中有新的Opportunity时,该触发器将创建一个新的Opportunity Owner
我是Salesforce的新手,我正在尝试创建一个触发器,它基本上会在每次添加新的Opportunity时更新字段并创建一个新的Opportunity owner 为清楚起见,我在下面附上了我的代码:Triggers 创建一个触发器,每当Salesforce中有新的Opportunity时,该触发器将创建一个新的Opportunity Owner,triggers,salesforce,Triggers,Salesforce,我是Salesforce的新手,我正在尝试创建一个触发器,它基本上会在每次添加新的Opportunity时更新字段并创建一个新的Opportunity owner 为清楚起见,我在下面附上了我的代码: trigger trig_Opportunity_CreateOppOwner on Opportunity (before insert, before update) { //Opportunity OppOwner = null; List<id>OppsI
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.
这基本上就是我要做的:
触发器更新前、插入前{
获取所有触发的机会。
验证旧Opportunity是否已具有匹配的所有者。
如果不是匹配的所有者,请更新Opportunity字段并更新Opportunity。
我不知道如何从第2步到第3步。任何帮助都将不胜感激。由于您提供的代码不清楚它在哪里完成,从我的角度来看,似乎在几行之后,您提供了另几行。您可以发布您的所有代码吗?如果不是这样,那么您发布的代码就是您的所有代码,f从我的角度来看,这段代码什么都不做,绝对没有 为什么? 没有验证的错误 没有DML操作
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
}
*/
// 3 started lines might be replaced by the following one
List<id>OppsID = Trigger.newMap.getKeys();
//but the following code perform select on Opportunity object and return the same list as Trigger.new
// OppToUpdate == Trigger.new
// What for? May be you should work with this part on "after insert/update"
List<Opportunity>OppToUpdate = [SELECT Id,
Name,
Owner__c,
OppOwner,
FROM Opportunity
//WHERE Id IN: Opp.ID // Opp - isn't exist and it isn't list/set of id
WHERE Id IN OppsId // I guess you meant this
];
// 1.variable "opp" isn't exist here
// 2. "OppToUpdate.id" - you can't use list in this manner
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.
触发器在before上下文中运行,该上下文在将记录提交到数据库之前在内存中的记录上运行。在本例中,对触发器运行的记录所做的所有更改都将被调制,而无需执行DML语句。但是,您的其他注释是正确的。我指的是在其他对象上的DML,如活动、acc挂载、联系等。