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;
}
}`