Triggers in Salesforce- Apex Triggers
- akshay saxena
- Feb 2, 2023
- 3 min read
Salesforce's Apex trigger will help you automate a variety of processes. A Trigger is an Apex script that executes before or after data manipulation events, such as before or after records insert, update, or delete. Triggers are written to perform tasks that can’t be done by using point-and-click tools in Salesforce.
For example, if validating a field value or updating a field on a record.
What is Trigger Syntax?
trigger TriggerName on ObjectName (trigger_events) {
code_block
}
TriggerName is the name you want to give to your trigger.
ObjectName is the object on which the action needs to be performed.
trigger_events are the comma-separated list of one or more events such as:
Before insert: Using this event, the code block is executed before a new record is inserted.
Before update: When you use this event, the code gets executed before a new record is updated in the object.
Before delete: Using this event, the record gets deleted before the execution of the code block.
After insert: The code block gets executed first, and then the insertion of the record is done.
After update: In this event, the updating of a record is done after the execution of the code block.
After delete: When you’re using this event, you are able to delete a record after the execution of the code block.
After undelete: This event is used when the record that was sent to the Recycle Bin needs to be restored.
What are different type of Triggers?
There are two types of triggers:
Before triggers are used to perform a task before a record is inserted or updated or deleted. These are used to update or validate record values before they are saved to the database.
After triggers are used if we want to use the information set by Salesforce system and to make changes in the other records. are used to access field values that are set by the system (such as a record’s Id or LastModifiedDate field), and to affect changes in other records. The records that fire the after trigger are read-only.
What are context variables in triggers?
All triggers in Salesforce determine implicit variables that enable developers to access the runtime context so that they don’t need to define objects from their side. To access the records that caused the trigger to fire, context variables are used.
Here is list of context variables in triggers:
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. This sObject list is only available in insert, update, and undelete triggers, and the records can only be modified in before triggers.
newMap: A map of IDs to the new versions of the sObject records. This map is only available in before update, after insert, after update, and after undelete triggers.
old : Returns a list of the old versions of the sObject records. This sObject list is only available in update and delete triggers.
oldMap: A map of IDs to the old versions of the sObject records. This map is only available in update and delete triggers.
Trigger Example with Scenario:
1). Write a trigger on Account, when an account is inserted, automatically account billing address should populate into the account shipping address.
trigger populateShippingAddress on Account (before insert){
for(Account aObj : Trigger.new){
//We should first check whether the user is passing null data
if(aObj.BillingStreet!=null){
aObj.ShippingStreet=aObj.BillingStreet;
}
if(a.BillingCity!=null){
aObj.ShippingCity=aObj.BillingCity;
}
if(a.BillingState!=null){
aObj.ShippingState=aObj.BillingState;
}
if(a.BillingPostalCode!=null){
aObj.ShippingPostalCode=aObj.BillingPostalCode;
}
if(a.BillingCountry!=null){
aObj.ShippingCountry=aObj.BillingCountry;
}
}
}
Note: If we use ” after insert ” then the fields of the account we are inserting will become read-only and we will not be able to update that account. This is the reason we are using ” before insert “.
Comentarios