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
renu anamalla 9renu anamalla 9 

Before insert and After insert example on Account

Im very confused on both please explain
Demand Chain SystemsDemand Chain Systems
Hi there,

Before insert and after insert are ways to utilize code with different steps of the lifecycle of a record save.  The primary differences are that with before insert, you can change values without having to save to the database.  However, when the trigger is in the before insert state, you don't have access to the id of the record yet.  With after insert, you need to perform DML if you want to update, delete, or insert additional records.  However, with after insert you DO have access to the Id of the record(s).

Please take a look at the following documentation for more information:
  1. Triggers and Order of Execution:
  2. Trigger Context Variables:
Hope that helps,
- James Loghry
Vasani ParthVasani Parth
Renu - Depends on your usage. Here's what I've learned :

  • I'm updating the record that's being updated/inserted - or doing something based on the record being modified
  • Examples: Set value of a pick list based on criteria. Send apex e-mail based on the record updated/inserted
  • I'm updating or creating records that are NOT being updated/inserted
  • Examples: Create a task of an Opportunity that's been edited, Change a look up value on a related record from the Opportunity being edited
  • The main thing to consider is that the Before happens before the data has been written to the server. This means you can modify the records in "" without having to call a separate "Update." This is ideal if you want to modify data in the records within
  • After happens after the data has been written to the server. This is important when wanting to create additional related records (Can't create a related record until AFTER the parent has been inserted).
  •  It's very rare that I need a trigger that runs AFTER.
Please mark this as the best answer if this helps