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
renaiah anamalla 6renaiah anamalla 6 

When will use the Before insert and after insert

1.When will use the Before insert and after insert?
2.What is the use of before upadte and after update?
3.what is context variables?
4.what is the difference b/w trigger.new and trigger.old

 
Ruwantha  LankathilakaRuwantha Lankathilaka

1.When will use the Before insert and after insert? 

There are two types of triggers: Before triggers and after triggers.
Before triggers are used to update or validate record values before they’re saved to the database.
After triggers are used to access field values that are set by the system (such as a record's Id or LastModifiedDatefield), and to effect changes in other records, such as logging into an audit table or firing asynchronous events with a queue. The records that fire the after trigger are read-only since it has completed the DB operations.
In your case you can use before insert to update  or validate the records that you are going to insert and after triggers can be used to use the saved data to another process.


2.What is the use of before update and after update?
You can use before update to change/validate  records before you are update and use after update to access the system set or to get calculated values.


3.what is context variables?
All triggers defined implicit variables that allow developers to access run-time context(identify the execution context of a trigger). These variables are contained in theSystem.Trigger class. 

Widely use new,old, newMap, oldMap are some of the context variables. Goto https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_context_variables.htm for more details.

4. what is the difference b/w trigger.new and trigger.old
Trigger.new returns the current version of the execution list of objects where as old returns the before state of the objects list.

jyothsna reddy 5jyothsna reddy 5
Hi Renaiah,

Context Variables:
All triggers define implicit variables that allow developers to access run-time context. These variables are contained in the System.Trigger class.

isExecuting
Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an executeanonymous() API call.

isInsert
Returns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.

isUpdate
Returns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.

isDelete
Returns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.

isBefore
Returns true if this trigger was fired before any record was saved.

isAfter
Returns true if this trigger was fired after all records were saved.

isUndelete
Returns true if this trigger was fired after a record is recovered from the Recycle Bin (that is, after an undelete operation from the Salesforce user interface, Apex, or the API.)

new
Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert and update triggers, and the records can only be modified in before triggers.

newMap
A map of IDs to the new versions of the sObject records. Note that this map is only available in before update, after insert, and after update triggers.

old
Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update and delete triggers.

oldMap
A map of IDs to the old versions of the sObject records. Note that this map is only available in update and delete triggers.

size
The total number of records in a trigger invocation, both old and new.

To avoid multiple triggers on the same object we can write single trigger and use  Context variables in it.

Trigger.new
: Returns a list of the new versions of the sObject records. Note that this sObject list is only available in insert andupdate triggers, and the records can only be modified in before triggers.
Trigger.old : Returns a list of the old versions of the sObject records. Note that this sObject list is only available in update anddelete triggers.

trigger.new is holding your object record data which is currently set by user and trigger.old contains history object records. like if we have Opportunity trigger. and in this Opportunity.trigger we have even after update and if you need to compute if there is any change in Opportunity stage then you can figure out this changes using trigger.new and trigger.old values.


Before Trigger: Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation(Insert, update, delete) on these triggers.
These triggers are fired before the data is saved into the database.

After Trigger: After triggers are used to perform the logic on the related objects and these triggers are used access the fields values that are created by system (Ex: CreatedBy, LasteModifiedBy , Record Id etc..).

Hope it helps you.

Regards,
Jyothsna D