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
Janakiraman E 5Janakiraman E 5 

trigger before insert

Hi All,

Regarding before insert or update trigger:
Technically, if we insert/update a value in a table we have to commit so that the values are added to DB. and then only we compare, fetch, update the values.
In Before insert or update, even before the values are commited to DB how salesforce validate/fetch the values and 'before' scenario is executed.

Please clarify. If possible can i have a flowchart how trigger is working behind the scenes.
Rakesh51Rakesh51
Depends on your usage. From my experience here's the break down:
Before
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
After
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 "Trigger.new" without having to call a separate "Update." This is ideal if you want to modify data in the records within Trigger.new
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).
I find about 95% of all the triggers I have ever written are Before Update/Insert. It's very rare that I need a trigger that runs AFTER.