Apex triggers help you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions.
Syntax
trigger triggerName on ObjectName (trigger_events) { Trigger_code_block }
Executing the Trigger
Following are the events on which we can fire the trigger −
Insert: This will run on Before and After conditions.
Update: This will run on Before and After conditions.
Delete: This will run on the Before condition only.
Upsert: This will run on Before and After only.
Undelete: This will run on After only.
Apex Trigger Example 1
Suppose we received a business requirement -
Write a trigger to create related contact when an Account is created and associate that contact with that account.
For this, we will create a trigger on the Account object
Trigger context - after insert
Explanation -
after - We use after in context because when the account is created after that we need to create a contact record.
Insert - We use insert because the trigger is firing after the account record is inserted,
Trigger -
trigger Contact_AI on Account (after insert) {
CreateContactHelper.addContact(trigger.new);
}
Helper Class -
public class CreateContactHadler{
public void addContact(List<Account> accounts){
List<Contact> contacts = new List<Contact>();
for(Account acc : accounts){
Contact con = new Contact();
con.LastName = acc.Name;
con.AccountId = acc.Id;
contacts.add(con);
}
if(contacts.size() > 0){
insert contacts;
}
}
}
Apex Trigger Context Variables -
Variable | Usage |
isExecuting | Returns true if the current context for the Apex code is a trigger, not a Visualforce page, a Web service, or an execute anonymous() 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. This recovery can occur 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 the update, and after undelete triggers. |
old | Returns a list of the old versions of the sObject records. This sObject list is only available in updates and deletes triggers. |
oldMap | A map of IDs to the old versions of the sObject records. This map is only available in updates and deletes triggers. |
operationType | Returns an enum of type System.TriggerOperation corresponds to the current operation. Possible values of the System.TriggerOperation enum are: BEFORE_INSERT, BEFORE_UPDATE, BEFORE_DELETE,AFTER_INSERT, AFTER_UPDATE, AFTER_DELETE, and AFTER_UNDELETE. If you vary your programming logic based on different trigger types, consider using the switch statement with different permutations of unique trigger execution enum states. |
size | The total number of records in a trigger invocation, both old and new. |
Apex Trigger Context Table
TriggerEvents | New | New Map | Old | Old Map | Can change field using Trigger.new | Can updates using DML operation | Can delete using DML operation |
Before Insert | (Original Record is not created) | (Original Record is not created) | |||||
After Insert | (Read Only) | (Record are available to update) | (Record are available to delete) | ||||
Before Update | (Update process is still running) | (Update process is still running) | |||||
After Update | (Read Only) | ||||||
Before Delete | (Not Available) | (The update are saved before its deletion so If undelete the update is visible) | (The deletion is already in progress) | ||||
After Delete | | | (Not Available) | (Object already been deleted) | (Object already been deleted) | ||
After Undelete | (Read Only) |
Order Of Execution
Refer to this link for the image - Trigger Order of Execution
Refer to this link for an explanation - Apex Trigger and Order Of Execution
Happy!! to help you add to your knowledge. You can leave a comment to help me understand how the blog helped you. If you need further assistance, please contact us. You can click "Reach Us" on the website and share the issue with me.
Blog Credit:
N. Mehra
Salesforce Developer
Avenoir Technologies Pvt. Ltd.
Reach us: team@avenoir.ai
Yorumlar