• Will Yee
  • NEWBIE
  • 20 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 3
    Replies
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;
}


Currently the trigger is creating 6 duplicate copies of the Opportunity instead of 1. Everything else seems to be working as intended though. 

It looks like its running multiple times through the second loop and adding 6 copies to the list, but shouldn't the "LIMIT 1" in the first section limit the "oppList" size to 1 record and the next 'for' loop only fire for one instance?

trigger insertNewOpp on Opportunity (after update) {

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

for(Opportunity opp : Trigger.new)
{
    //Make a list of all opps where Id is equal to that of the current opp
    List<Opportunity> oppList = [SELECT Id,AccountID,StageName,Primary_Contact__c, LeadSource, ForecastCategoryName, Name, CloseDate, RecordTypeId
        FROM Opportunity
        WHERE ID = :opp.Id
        LIMIT 1];
       
for(Opportunity oppi : oppList){
//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=oppi.AccountID, StageName='Prospecting', Primary_Contact__c=oppi.Primary_Contact__c, LeadSource=oppi.LeadSource, ForecastCategoryName='Omitted', Name=oppi.Name + 'New', CloseDate=oppi.CloseDate, RecordTypeId = '012d0000000SocP'); 
           //Add to the list
          listOpp.add(oppNew);
       }
       }
}
     
     if(listOpp.size() > 0)
     insert listOpp;
}
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;
}


Currently the trigger is creating 6 duplicate copies of the Opportunity instead of 1. Everything else seems to be working as intended though. 

It looks like its running multiple times through the second loop and adding 6 copies to the list, but shouldn't the "LIMIT 1" in the first section limit the "oppList" size to 1 record and the next 'for' loop only fire for one instance?

trigger insertNewOpp on Opportunity (after update) {

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

for(Opportunity opp : Trigger.new)
{
    //Make a list of all opps where Id is equal to that of the current opp
    List<Opportunity> oppList = [SELECT Id,AccountID,StageName,Primary_Contact__c, LeadSource, ForecastCategoryName, Name, CloseDate, RecordTypeId
        FROM Opportunity
        WHERE ID = :opp.Id
        LIMIT 1];
       
for(Opportunity oppi : oppList){
//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=oppi.AccountID, StageName='Prospecting', Primary_Contact__c=oppi.Primary_Contact__c, LeadSource=oppi.LeadSource, ForecastCategoryName='Omitted', Name=oppi.Name + 'New', CloseDate=oppi.CloseDate, RecordTypeId = '012d0000000SocP'); 
           //Add to the list
          listOpp.add(oppNew);
       }
       }
}
     
     if(listOpp.size() > 0)
     insert listOpp;
}