I have a trigger on the OpportunityLineItem object that we use to update 4 revenue fields with specific calculations when the record is either created or updated. For some reason it seems the trigger is not always firing when records are created. Below is the trigger's code, which I modified to use the same calculation of "oli.UnitPrice * oli.Quantity" for each field update to make it easier to read here... the real calculations are a bit long.
trigger RevenueBookingsHandler_TRG on OpportunityLineItem (after insert, after update) {
List<OpportunityLineItem> oliUpdate = new list<OpportunityLineItem>();
for(OpportunityLineItem oli : Trigger.New) {
if(Recursivehandler.runOnce()) {
// Default
if(oli.Calculation_Type__c == 'Default' || test.IsRunningTest()) {
OpportunityLineItem oliCalc = new OpportunityLineItem();
oliCalcId = oli.Id;
oliCalcNet_New_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcNet_Revenue_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Non_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcAnnual_Contract_Value__c = oli.UnitPrice * oli.Quantity;
if(!test.IsRunningTest()) oliUpdate.add(oliCalc);
}
// CC Product
if(oli.Calculation_Type__c == 'CC Product' || test.IsRunningTest()) {
OpportunityLineItem oliCalc = new OpportunityLineItem();
oliCalcId = oli.Id;
oliCalcNet_New_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcNet_Revenue_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Non_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcAnnual_Contract_Value__c = oli.UnitPrice * oli.Quantity;
if(!test.IsRunningTest()) oliUpdate.add(oliCalc);
}
// Registration & Flat Fee
if(oli.Calculation_Type__c == 'Registration & Flat Fee' || test.IsRunningTest()) {
OpportunityLineItem oliCalc = new OpportunityLineItem();
oliCalcId = oli.Id;
oliCalcNet_New_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcNet_Revenue_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Non_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcEst_Recurring_Bookings_Calc__c = oli.UnitPrice * oli.Quantity;
oliCalcAnnual_Contract_Value__c = oli.UnitPrice * oli.Quantity;
if(!test.IsRunningTest()) oliUpdate.add(oliCalc);
}
}
}
if(!oliUpdate.isEmpty()) Update oliUpdate;
}
I'm fairly new to apex coding, and have a feeling it might be related to the recursive handler I have in there. This was added as I was dealing with some "maximum trigger depth exceeded" issues when testing the trigger.
Any thoughts as to why this does not always fire on record creation, or what I might be able to change to make the code more reliable?
UPDATE:
Adding the Recursivehandler code below.
public Class Recursivehandler{
private static boolean run = true;
public static boolean runOnce(){
if(run){
run=false;
return true;
}else{
return run;
}
}
}
Recursivehandler.runOnce()
do? – David Reed♦ Feb 20 '18 at 15:29OpportunityLineItem
? Your naming convention for this trigger implies you are not followingOne Trigger Per Object
. – Adrian Larson♦ Feb 20 '18 at 15:38OpportunityLineItem
records (or if you use it in another part of the apex transaction that involves this trigger, or several other conditions). Please edit your question to include the code forRecursivehandler
, and if you can, include more details about the volume of records you're handling at a given time. – Derek F Feb 20 '18 at 15:46