function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
joseph keuler 1joseph keuler 1 

Record locked when updating value via Apex when triggered from Process Builder

So I have an Apex method running off the process builder but when I try to update a record via the invoked method it says the record is locked. Perhaps the reason it won’t update is because the process hasn’t completed yet? How can I update a record via Apex triggered from the process builder?
 
@invocablemethod
public static void updateBenefit(list<ea_app__c> app){
                app[0].Benefit_Amount__c = calcBenefit(app[0].name);
    // ----> this is the issue here  <----
    update app;
}

public static decimal calcBenefit(string appID){
    decimal benefit = 0.0;
    integer coeff = getCoefficient('2015');
    decimal max = getMax(getfamilySize(appID));
    benefit = (1-(getIncome(appID)/max))*(getFuelCost(appID)/coeff);
    return benefit;        
}

 
Best Answer chosen by joseph keuler 1
joseph keuler 1joseph keuler 1
Thanks for responding.  You are correct that concurrent access to the object is happening (incidentially I was calling a flow).  What I ended up doing was firing a trigger that modified before update (and insert).  Updating a calling object from Apex via Process builder does not work because all of the tiggers are AFTER update.  They need to be before update.

All Answers

James LoghryJames Loghry
Since you're issuing an update on the same record while your process is in the middle of executing, it's throwing the locked error.  Instead, I might try calling a Flow from your process instead.  You can still use the same InvocableMethod, but it should be in a different context and avoid the locking error is my assumption.
joseph keuler 1joseph keuler 1
Thanks for responding.  You are correct that concurrent access to the object is happening (incidentially I was calling a flow).  What I ended up doing was firing a trigger that modified before update (and insert).  Updating a calling object from Apex via Process builder does not work because all of the tiggers are AFTER update.  They need to be before update.
This was selected as the best answer