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
Rui Wang 7Rui Wang 7 

apex trigger not fired after workflow field update

HI, Salesforce experts:

We have following requirement. Salesforce sends out email, recipient replies to the email then salesforce updates the associated case status via workflow field update, at last case assignment rule should be triggered via apex trigger upon the case status update. All work well except the apex trigger is not fired, which breaks whole process and case assignment rule is not invoked at all. But based on the salesforce document, the workflow field update can fire the after trigger. Any idea?

Thank you!

Roy
 
Rui Wang 7Rui Wang 7
just to make it clear, the trigger is built on Case object. Workflow rule is on Email Message object, but to update associated case status. 
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hello,
Excerpt of Salesforce Order of Execution:
  1. Loads the original record from the database or initializes the record for an upsert statement.
  2. Loads the new record field values from the request and overwrites the old values. Salesforce runs some system validation rules depending on if request came from UI Edit page
  3. Executes all before triggers.
  4. Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any user-defined validation rules.
  5. Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record is not saved and no further steps, such as after triggers and workflow rules, are taken.
  6. Saves the record to the database, but doesn’t commit yet.
  7. Executes all after triggers.
  8. Executes assignment rules.
  9. Executes auto-response rules.
  10. Executes workflow rules.
  11. If there are workflow field updates, updates the record again.
  12. If the record was updated with workflow field updates, fires before update triggers and after update triggers one more time (and only one more time), in addition to standard validations. Custom validation rules and duplicate rules are not run again.
  before update and after update triggers will fire, then the workflow fires.

  Hope this helps you!
  If my answer helps resolve your question , please mark it as the 'Best Answer' & upvote it to benefit others.
  Thanks
Rui Wang 7Rui Wang 7
Hi, Muhammed, thanks for the reply. 

checked on the execution order, on rule 12, it says the apex trigger will fire one more time after workflow rule field update, any thought?
MUHAMMED SEMIN P NMUHAMMED SEMIN P N
Hi, Rui Wang 7
Use context variable such as Trigger.isInvokedByFieldUpdate
Sagar Wahal 1Sagar Wahal 1
Do you have some code in your trigger that stop recursive call? In-case you do, that might be stopping the trigger from running again.
Rui Wang 7Rui Wang 7
HI, Muhammed. thanks again. Does salesforce support the context variable like Trigger.isInvokedByFieldUpdate? i cannot find any clue but just an idea yet not be delivered yet. https://success.salesforce.com/ideaView?id=087300000007Unh
Rui Wang 7Rui Wang 7
Hi Sagar, thanks a lot for asking. Please find the sample code:

trigger xxxxx on Case (after update) {
    List<Case> caseList = new List<Case>();
    
    if(Trigger.isAfter && Trigger.isUpdate){
        for (Case caseObj : Trigger.new) {
            system.debug('start trigger iteraion');
            if(caseObj.IsEscalated == true && caseObj.IsEscalated != Trigger.oldMap.get(caseObj.id).IsEscalated){
                xxxxxxx
            }
            else if(caseObj.Status == 'Response Received' && caseObj.Categ__c == 'Account Administration' && Trigger.oldMap.get(caseObj.id).Status !='Response Received'){
                system.debug('Entity Tranfer Response Received: ' + caseObj.Id);
                caseList.add(new Case(id = caseObj.id));
            }   
        }
        
        if(caseList.size() != 0){
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule = true;
            Database.update(caseList, dmo);  
            system.debug('Assigment Finished');
        }

    }
}