Skip to main content

Command Palette

Search for a command to run...

salesforce trigger-

Updated
•3 min read•View as Markdown
salesforce trigger-

trigger- trigger is a piece of code that execute automatically before an update to perform action insert, update, delete, and undelete is called as a trigger.

trigger two types-

  1. Before

  2. After

Perform Action-

1)Insert.

2)Update.

3)Delete.

4)Undelete.

ContainsKey-

1)trigger.new

2)trigger.old

3)trigger.newMap

4)trigger.oldMap

5)trigger.isInsert

6)trigger.isDelete

7)trigger.isUndelete

8)trigger.isUpdate

Trigger Syntax-

trigger TriggerName on objectName(trigger_events){

code block

}

trigger.isInsert-return true the current context for the apex code is an Insert, not a visualforce page.

trigger.isUpdate-return true the current context for apex code is Update.

Trigger And 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 performs different validation checks depending on the type of request.

    • For requests from a standard UI edit page, Salesforce runs these system validation checks on the record:

      • Compliance with layout-specific rules

      • Required values at the layout level and field-definition level

      • Valid field formats

      • Maximum field length

Additionally, if the request is from a User object on a standard UI edit page, Salesforce runs custom validation rules.

  • For requests from multiline item creation such as quote line items and opportunity line items, Salesforce runs custom validation rules.

  • For requests from other sources such as an Apex application or a SOAP API call, Salesforce validates only the foreign keys and restricted picklists. Before executing a trigger, Salesforce verifies that any custom foreign keys don’t refer to the object itself.

  1. Executes record-triggered flows that are configured to run before the record is saved.

  2. Executes all before triggers.

  3. Runs most system validation steps again, such as verifying that all required fields have a non-null value, and runs any custom 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.

  4. Executes duplicate rules. If the duplicate rule identifies the record as a duplicate and uses the block action, the record isn’t saved and no further steps, such as after triggers and workflow rules, are taken.

  5. Saves the record to the database, but doesn't commit yet.

  6. Executes all after triggers.

  7. Executes assignment rules.

  8. Executes auto-response rules.

  9. Executes workflow rules. If there are workflow field updates:

    Note

    This sequence applies only to workflow rules.

    1. Updates the record again.

    2. Runs system validations again. Custom validation rules, flows, duplicate rules, processes, and escalation rules aren’t run again.

    3. Executes before update triggers and after update triggers, regardless of the record operation (insert or update), one more time (and only one more time)

  10. Executes escalation rules.

  11. Executes these Salesforce Flow automations, 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.

  1. Executes record-triggered flows that are configured to run after the record is saved

  2. Executes entitlement rules.

  3. 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 save procedure.

  4. If the parent record is updated, and a grandparent record contains a roll-up summary field or is part of across-object workflow, performs calculations and updates the roll-up summary field in the grandparent record. Grandparent record goes through save procedure.

  5. Executes Criteria Based Sharing evaluation.

  6. Commits all DML operations to the database.

  7. After the changes are committed to the database, executes post-commit logic are executed. Examples of post-commit logic (in no particular order) include:

    • Sending email

    • Enqueued asynchronous Apex jobs, including queueable jobs and future methods

    • Asynchronous paths in record-triggered flows

More from this blog

Untitled Publication

10 posts