[SalesForce] Trigger to update fields on record create or edit

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

Best Answer

In general, there is some overcomplication. Yes, the recursive piece is suspect, but I'm also looking at your code and wondering why you're doing the following:

You're trying to look at OLIs that are created or edited, and you want to update fields on those OLIs. Not on other ones. But your trigger is an after trigger, and you're also making new OLIs (yes, with the same ID, but still...) and are updating those records.

Perhaps you can change the trigger to a before trigger, and just update the fields you want on the OLIs based on whatever calculations you have to make.

Oh, and then you can see if your recursion problems (and as Adrian said, your probable lack of one trigger per object) are causing any hiccups.