0

Getting a recursive error when im trying to add a new OLI to my opportunity, this only comes up when i add a specific product called delegate pass, Ive tried to debug but it seems to hit the trigger multiple times before hitting the limit, i can't actually see where its trying to loop.

the error:

Apex trigger OpportunityLineItemTrigger caused an unexpected exception, contact your administrator: OpportunityLineItemTrigger: execution of BeforeInsert caused by: System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, OpportunityLineItemTrigger: maximum trigger depth exceeded OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new] OpportunityLineItem trigger event BeforeInsert for [new]: []: Class.opportunityLineItemHandler.addDelegatePasses: line 506, column 1

this is the trigger:

trigger OpportunityLineItemTrigger on OpportunityLineItem (before insert,before update,After insert) {

// Do not execute for any user that has the No validation flag set to true
CXT_Bypass_Settings__c notriggers = CXT_Bypass_Settings__c.getInstance(UserInfo.getUserId());
if (notriggers == null || !notriggers.CXT_No_Triggers__c) {
    if(trigger.isBefore && trigger.isInsert){
        for(OpportunityLineItem oli:trigger.New){
            system.debug(oli);
            oli.CXT_Asset__C = null;
        }
    }

    system.debug(opportunityLineItemHandler.Class);
    TriggerFactory.createAndExecuteHandler(opportunityLineItemHandler.Class);

}

}

trigger factory:

  public static void createAndExecuteHandler(Type t)
{
    // Get a handler appropriate to the object being processed
    TriggerInterface handler = getHandler(t);
     System.debug('handler '+handler);
     System.debug('handler type '+t);
    // Make sure we have a handler registered, new handlers must be registered in the getHandler method.
    if (handler == null)
    {
        throw new TriggerException('No Trigger Handler found named: ' + t.getName());
    }

    // Execute the handler to fulfil the trigger
    if(!isTriggerExecuted)
        execute(handler);
}

Handler function that is failing:`

 public static void addDelegatePasses(List<OpportunityLineItem> lstOlis){
    Set<Id> product2Ids = new Set<Id>();
    Set<String> productCodes = new Set<String>();
    Set<Id> priceBook2Ids = new Set<Id>();
    Set<Id> opportunityIds = new Set<Id>();
    List<OpportunityLIneItem> passesToAdd = new List<OpportunityLIneItem>();

    Map<Id,Product2> mapProduct2 = new Map<Id,Product2>();
    Map<Id,Opportunity> mapOpportunity = new Map<Id,Opportunity>();
    Map<String,List<PriceBookEntry>> delegateProductCodeToLstPBE = new Map<String,List<PriceBookEntry>>();

    for(OpportunityLineItem oli : lstOlis){
        product2Ids.add(oli.Product2Id);
        opportunityIds.add(oli.opportunityId);

    }
    for(Opportunity opp : [Select Id,CurrencyIsoCode,PriceBook2Id from Opportunity where Id In : opportunityIds]){
        priceBook2Ids.add(opp.PriceBook2Id);
        mapOpportunity.put(opp.Id,opp);
    }
    for(Product2 prod : [Select Id,Delegate_Pass_Included__c,
                                No_of_Delegate_Pass__c,
                                Delegate_Pass_Product_Code__c 
                                from Product2 
                                where Id In : product2Ids
                                and Delegate_Pass_Included__c =: true]){


        mapProduct2.put(prod.Id,prod);
        if(prod.Delegate_Pass_Product_Code__c != null || prod.Delegate_Pass_Product_Code__c != ''){
            productCodes.add(prod.Delegate_Pass_Product_Code__c);
        }
    }
    if(productCodes.size()>0){
        for(PriceBookEntry pbe : [Select Id,Product2Id,
                                CurrencyIsoCode,
                                Product2.ProductCode,
                                UnitPrice,PriceBook2Id
                                from PriceBookEntry 
                                where Product2.ProductCode In : productCodes
                                and PriceBook2Id In : priceBook2Ids]){

            List<PriceBookEntry> lstPBE = delegateProductCodeToLstPBE.get(pbe.Product2.ProductCode);
            if(lstPBE == null){
                lstPBE = new List<PriceBookEntry>();
            }
            lstPBE.add(pbe);
            delegateProductCodeToLstPBE.put(pbe.Product2.ProductCode,lstPBE);
        }
    }
    for(OpportunityLineItem oli : lstOlis){
        Product2 prod = mapProduct2.get(oli.Product2Id);
        Opportunity Opp = mapOpportunity.get(oli.OpportunityId);
        if(prod != null && prod.Delegate_Pass_Product_Code__c != null){
            List<PriceBookEntry> lstPBEs = delegateProductCodeToLstPBE.get(prod.Delegate_Pass_Product_Code__c);
            if(lstPBEs != null){
               PriceBookEntry Pbentry = new PriceBookEntry();
               for(PriceBookEntry pbe : lstPBEs){
                    if(pbe.CurrencyIsoCode == Opp.CurrencyIsoCode && pbe.PriceBook2Id == Opp.PriceBook2Id ){
                        Pbentry =  pbe;
                        break;
                    }
                }
                if(Pbentry.Id != null){
                    OpportunityLineItem delegatePass = new OpportunityLineItem();
                    delegatePass.opportunityId = opp.Id;
                    delegatePass.PriceBookEntryId = Pbentry.Id;
                    delegatePass.UnitPrice = Pbentry.UnitPrice;
                    delegatePass.Quantity = prod.No_of_Delegate_Pass__c;
                    passesToAdd.add(delegatePass);
                }
            }
        }
    }
    system.debug(passesToAdd.size());
    if(passesToAdd.size()>0){
        insert passesToAdd;
    }
}`
0
insert passesToAdd;

What's happening here is that this trigger is adding new opportunity line items, which in turn are adding new opportunity line items, which in turn are adding new opportunity line items...

It would take me a few minutes to map out exactly what's going on (hint: make sure you add comments to your code), but basically, you'll want to make sure that your logic isn't trying to add the same products over and over again. You can probably at least partially fix this by changing the before insert trigger to after insert instead, query existing line items, and if they don't already exist, then insert them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.