[SalesForce] Run before update trigger on after insert

So I have a trigger that I want to run everytime a record is updated and created. However, when I use after insert and before update I get a record is locked error when creating the record.

How can I run this trigger on both record edit and creation?

Better yet, is it possible to update any field "after update" or "after insert"?

trigger CalcBenefit on EA_App__c (before update, after insert) {
//works for update only.  insert throws error because I'm trying to change value
for (EA_App__C ea : Trigger.new){

    EA_App__C oldEA = Trigger.oldMap.get(ea.Id);

    if (ea.X3_Month_Income__c > CalculateBenefit.getMax(integer.valueOf(ea.Family_Size__c))){
        ea.App_Status__c = 'DENIED';            
        ea.Benefit_Amount__c = 0;                 

    }else if (ea.X3_Month_Income__c <= CalculateBenefit.getMin(integer.valueOf(ea.Family_Size__c)) && ea.App_Status__c != 'PAID'){
        //how to compare to old? 
        //&& (ea.X3_Month_Income__c != oldEA.X3_Month_Income__c || ea.Family_Size__c != oldEA.Family_Size__c)
        ea.Benefit_Amount__c = 0;                 
        ea.App_Status__c = 'SENT FOR APPROVAL';                

    }else{
        ea.App_Status__c = 'PAID';
        ea.Benefit_Amount__c = CalculateBenefit.calc(ea.X3_Month_Income__c, integer.valueOf(ea.Family_Size__c), ea.Fuel_Cost__c, '2015');
    }

}

}

Best Answer

You can use before insert to modify records before they're saved to the database, which should work well enough for your purposes.

You can simply set oldEA to a blank record, which will also satisfy your "changed fields" conditions:

EA_App__C oldEA = Trigger.isInsert? new EA_App__c(): Trigger.oldMap.get(ea.Id);

If it doesn't work, you can also choose to update the records recursively:

if(Trigger.isAfter && Trigger.isInsert) {
    update Trigger.new.deepClone(true);
} else {
    // Before Update Logic
}