You need to sign in to do that
Don't have an account?
Cloning opportunity related to OpportunityLineItem
Hello Everyone,
I have got the following trigger on opportunity which should check if the opportunity stage = closedwon, product2.family = membership (Picklist). If this critera is matched with an opportunity it gets cloned and stage gets assigned to 'Completed' and closedate = date+365 days:
Code:
trigger OpportunityTester on Opportunity (after update) { List<Opportunity> oppwithproduct = [select id,(select id From OpportunityLineItems where Pricebookentry.product2.Family = 'membership') from Opportunity where StageName = 'Closed Won']; Oppwithproduct = Trigger.new; OpportunityFetcher.Opportunityreplicate(Oppwithproduct); }
public class OpportunityFetcher{ public static void Opportunityreplicate(List<Opportunity> Oppwithproduct){ Set<id> OppIDs = new set<id>(); // Creating a set for storing Opportunity ids List<Opportunity> oppsToClone = new List<Opportunity>{}; for(Opportunity opp:Oppwithproduct){ //oppsToClone.add(newopp); Opportunity newOpp = new Opportunity(); newOpp = opp.clone(false, true); newopp.Name = opp.Name; newopp.StageName = 'Completed'; newopp.CloseDate = (opp.CloseDate+365); oppsToClone.add(newopp); } insert oppsToClone; } }
I am getting following error:
Validation Errors While Saving Record(s)
There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger OpportunityTester caused an unexpected exception, contact your administrator: OpportunityTester: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityTester: maximum trigger depth exceeded Opportunity trigger event AfterUpdate for [006c0000007wqqI] Opportunity trigger event AfterUpdate for [006c0000007wqqJ] Opportunity trigger event AfterUpdate for [006c0000007wqqK] Opportunity trigger event AfterUpdate for [006c0000007wqqL] Opportunity trigger event AfterUpdate for [006c0000007wqqM] Opportunity trigger event AfterUpdate for [006c0000007wqqN] Opportunity trigger event AfterUpdate for [006c0000007wqqO] Opportunity trigger event AfterUpdate for [006c0000007wqqP] Opportunity trigger event AfterUpdate for [006c0000007wqqQ] Opportunity trigger event AfterUpdate for [006c0000007wqqR] Opportunity trigger event AfterUpdate for [006c0000007wqqS] Opportunity trigger event AfterUpdate for [006c0000007wqqT] Opportunity trigger event AfterUpdate for [006c0000007wqqU] Opportunity trigger event AfterUpdate for [006c0000007wqqV] Opportunity trigger event AfterUpdate for [006c0000007wqqW] Opportunity trigger event AfterUpdate for [006c0000007wqqX]: []: Class.OpportunityFetcher.Opportunityreplicate: line 14, column 1".
Any help guys??
Maf_007
Your statement:
will cause your after update trigger to fire again and thus reclone and reclone ....
You need to prevent trigger recursion. Consult this recipe: http://developer.force.com/cookbook/recipe/controlling-recursive-triggers
All Answers
Maf_007
Your statement:
will cause your after update trigger to fire again and thus reclone and reclone ....
You need to prevent trigger recursion. Consult this recipe: http://developer.force.com/cookbook/recipe/controlling-recursive-triggers