function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Nate GriebNate Grieb 

Update trigger on account to create opportunity is creating two opportunities.

I am trying to create a trigger to create an opportunity on an account when their "billing state" is == trial. Right now, everytime the account is updated to trial it is creating 2 opportunites. Here is the trigger:

trigger Opportunity_inTrial on Account (before update, after update) {
    List<opportunity>Listopp = New List<opportunity>();
    for(account acc_old:trigger.old){
       if(acc_old.Billing_State__c != 'trial'){
           for(account acc:trigger.new){
            if(acc.Billing_State__c == 'trial'){    
                Opportunity opp = New opportunity();
                opp.accountid = acc.id;
                opp.name=acc.name;
                opp.CloseDate=date.Today()+14;
                opp.StageName='0 VO Score';
                Listopp.add(opp);
               }
           }
       }
    }
    if(Listopp.size()>0 && Listopp.size()<2)
    insert Listopp;
}
Best Answer chosen by Nate Grieb
Geoffrey J FlynnGeoffrey J Flynn
Hi Nate,

Your trigger is firing both before and after update of an account, which is causing it to run twice.  Take out the after update from line 1 and you should be fine.