You need to sign in to do that
Don't have an account?

Trigger - Inserts new Opp after old Opp changes stage
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;
}
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;
}
All Answers
Hi Will,
Could you mark this question as resolved and possibly pick best answer.
Thanks,
Deepak.