Skip to main content

Order of Execution of a record in Salesforce Database

  • Loads the original record from the database or initializes the record for an upsert statement.
  • Loads the new record field values from the request and overwrites the old values.
    • If the request came from a standard UI edit page, Salesforce runs system validation to check the record for:

      • Compliance with layout-specific rules
      • Required values at the layout level and field-definition level
      • Valid field formats
      • Maximum field length
    • When the request comes from other sources, such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys. Before executing a trigger, Salesforce verifies that any custom foreign keys do not refer to the object itself.
    • Salesforce runs user-defined validation rules if multiline items were created, such as quote line items and opportunity line items.
  • Executes record-triggered flows that are configured to run before the record is saved.
  • Executes all before triggers.
  • 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. The only system validation that Salesforce doesn't run a second time (when the request comes from a standard UI edit page) is the enforcement of layout-specific rules.
  • 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.
  • Saves the record to the database, but doesn't commit yet.
  • Executes all after triggers.
  • Executes assignment rules.
  • Executes auto-response rules.
  • Executes workflow rules.
  • Executes escalation rules.
  • If there are workflow field updates, updates the record again.
  • If the record was updated with workflow field updates, fires before update triggers and after update trigger one more time (and only one more time), in addition to standard validations. Custom validation rules, flows, duplicate rules, processes, and escalation rules are not run again.

Note: The refiring of triggers isn't limited to updates but applies to all operation types. A workflow field update that fires on record insert will rerun any before and after insert triggers again—as insert triggers.


  • Executes the following Salesforce Flow automation, but not in a guaranteed order.
    • Processes
    • Flows launched by processes
    • Flows launched by workflow rules (flow trigger workflow actions pilot)
                When a process or flow executes a DML operation, the affected record goes through the save procedure.

  • Executes entitlement rules.
  • Executes record-triggered flows that are configured to run after the record is saved.
  • If the record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the parent record. Parent record goes through the save procedure.
  • If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of a cross-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through the save procedure.
  • Executes Criteria Based Sharing evaluation.
  • Commits all DML operations to the database.
  • Executes post-commit logic, such as sending the email.
Note: During a recursive save, Salesforce skips steps 9 (assignment rules) through 19 (roll-up summary field in the grandparent record).

Comments

Popular posts from this blog

Static and Instance Methods, Variables, and Initialization Code

Static methods, variables, and initialization code have these characteristics They’re associated with a class. They’re allowed only in outer classes. They’re initialized only when a class is loaded. They aren’t transmitted as part of the view state for a Visualforce page. Using Static Methods and Variables public class Parent {     public static boolean isRun = true;  } A trigger that uses this class could then selectively fail the first run of the trigger trigger T1 on Account (before delete, after delete, after undelete) {         if(Trigger.isBefore){           if(Trigger.isDelete){              if( Parent .isRun){                  Trigger.old[0].addError('Before Account Delete Error');                    Parent .isRun =false;              }  ...

Salesforce Interview Trigger Questions

1. Write a trigger on Account, for update related contacts if Contact's Company Name field is blank, populate it with Account's Company Name field. Limitations: Only 1 loop allowed. Solution: trigger on Account (After Update) {      Map<Id, Account> accountMap = Trigger.newMap;      List<Contact> contactList = [Select Id, Company_Name__c From Contact Where AccountId                in:accountMap.keys()];      List<Contact> contactListForUpdate = List<Contact>();      for(Contact contact : contactList) { if(String.isBlank(contact.Company_Name__c)) {      contact.Company_Name__c = accountMap.get(contact.AccountId).Company_Name__c;      contactListForUpdate.add(contact); } } update contactListForUpdate; }