每次Salesforce中有新的商机时,创建一个将创建新商机所有者的触发器

时间:2013-05-30 20:30:50

标签: triggers salesforce

我是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. 如果它不是匹配的所有者,请更新商机字段并更新商机。
  4. 我不知道如何从第2步到第3步。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分: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.