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
Will YeeWill Yee 

Trigger Inserts new Opp after old Opp changes Stage to Closed Won

Hi, currently the trigger is creating two duplicate Opportunity records instead of a single one. I have disabled all other triggers on the Opportunity object, but I still can't see to see why this would be creating two instead of one on Stage change.

Would love to see if someone could take a quick look and point me in the right direction

trigger insertNewOpp2 on Opportunity (after update) {

//create list of Opps
List<Opportunity> listOpp = new List<Opportunity>();

for(Opportunity opp : Trigger.new)
{
       
//if Opp is changed to Closed Won
   if(opp.StageName == 'Closed Won')
       {
           //Create a new opp with the following attributes
          Opportunity oppNew = new Opportunity (AccountID=opp.AccountID, StageName='Prospecting', Primary_Contact__c=opp.Primary_Contact__c, LeadSource=opp.LeadSource, ForecastCategoryName='Omitted', Name=opp.Name + 'New', CloseDate=opp.CloseDate, RecordTypeId = '012d0000000SocP'); 
           //Add to the list
          listOpp.add(oppNew);
       }
       }
     if (listOpp.size() > 0)
     insert listOpp;
}


Best Answer chosen by Will Yee
Gigi.OchoaGigi.Ochoa
Do you have a workflow field update that is also updating the Opportunity when the Opportunity is Closed.  That could be causing your trigger to run again.  You should probably create a static Boolean variable to prevent recursive triggers.

Here is an example on how to prevent recursive triggers:
http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)

All Answers

Gigi.OchoaGigi.Ochoa
Do you have a workflow field update that is also updating the Opportunity when the Opportunity is Closed.  That could be causing your trigger to run again.  You should probably create a static Boolean variable to prevent recursive triggers.

Here is an example on how to prevent recursive triggers:
http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US (http://help.salesforce.com/apex/HTViewSolution?id=000133752&language=en_US)

This was selected as the best answer
Will YeeWill Yee
This was exactly it, thanks so much Gigi!