0

我是 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.

这基本上就是我想要做的:触发器(更新前,插入前){

  1. 获取所有触发的机会。
  2. 验证旧机会是否已有匹配的所有者。
  3. 如果不是匹配的所有者,请更新商机字段并更新商机。

我不确定如何从第 2 步到第 3 步。任何帮助将不胜感激。

4

1 回答 1

0

根据您提供的代码并不清楚它在哪里完成,从我的角度来看,在您提供的几行之后,存在另外几行。你能发布你所有的代码吗?如果不是这样,并且您发布的代码就是您的所有代码,那么从我的角度来看,此代码什么也不做,绝对什么也不做。

为什么?

  • 没有验证的错误
  • 无 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.
    
于 2013-05-31T06:01:43.123 回答