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
WarjieWarjie 

Issue with order of execution of triggers and workflows when DML is fired inside a VF page

Hi, I have read the order of execution from Salesforce (https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_order_of_execution.htm) and it seems like contradicting to what I'm getting with my test.

Please see below timeline of events/results of my test:
  1. Save button (saveAdjustments method) in a VF page is clicked.
  2. Inside the code, it sets the Approval Status of Transaction Adjustment (TA) to "Pending".
  3. It will insert a new TA record.
  4. Workflow rule will run: Update Case Status on Transaction Adjustment
  5. Workflow will update the Approval Status of TA to "Awaiting Approval".
  6. After the TA has been created, I noticed that the TA's Approval Status is now back to "Pending" even though the rule was successfully met.
Here's the debug and it seems that the rule was called first before the save method was invoked.

User-added image
This is a portion of the code where it sets the status to "Pending" as a default value:
public PageReference saveAdjustments(){
        
        // Set up list for insertion
        Decimal appLimit = getApprovalAmountLimit();
        Decimal secLimit = getSecondApprovalAmountLimit();
        list<Transaction_Adjustment__c> taToInsert = new list<Transaction_Adjustment__c>();
        for (AdjustmentWrapper item : adjustmentList){
            item.ta.Approval_Status__c = pendingStatus; //pendingStatus = 'Pending'
Notes:
  1. I cannot simply comment out the assignment of Approval Status in the code as it is being used in the creation of another record from a different object. Doing so caused me to experience a null reference error.
  2. I cannot also change the status to "Awaiting Approval" in the code as I don't know the full impact. I;m not the one who created this.
MAIN QUESTION:
- Is there a way to make the Approval Status to "Awaiting Approval" via workflow with at least minimal change(s)?

Appreciate your response everyone! Thanks!