Apex triggers permit you to perform custom actions before or after events to data in Salesforce, which include insertions, updates, or deletions, like database systems support triggers, Apex provides trigger support for managing records.
Table of Contents
When to use Apex Triggers in Salesforce?
- Use triggers to perform operations based on specific conditions, to modify related data or restrict certain operations from happening.
- Moreover , you can use apex triggers to do whatever you may do in Apex, which include executing SOQL and DML or calling custom Apex methods.
- Triggers can perform tasks that can’t be completed through using the point-and-click tools withinside the Salesforce user interface. For example, if validating a field value or updating a field on a record, use validation rules and workflow rules instead.
- You can have a trigger run before an object’s records are inserted into the database, after records have been deleted, or even after a record is restored from the Recycle Bin.
- You can define triggers for top-level standard objects that support triggers, such as a Contact or an Account, some standard child objects, such as a CaseComment, and custom objects.
For more details refer to official link.
Apex Triggers Syntax
The syntax of a apex trigger definition isn’t the same as a class definition syntax. A trigger definition starts with the trigger keyword. Then name of the trigger, the Salesforce object that the trigger is related with, and the conditions under which it fires. A trigger has the subsequent syntax:
trigger TriggerName on ObjectName (trigger_events) {
code_block
}
To execute a trigger before or after ,insert, update, delete, and undelete operations, specify more than one trigger events in a comma-separated list. The events you may specify are:
- before insert
- after insert
- before update
- after update
- before delete
- after delete
- after undelete
For example, the following code defines a trigger for the before insert and before update events on the Account object:
trigger myAccountTrigger on Account (before insert, before update) {
// Your code here
}
Notes:
- The code block of a trigger cannot contain the static keyword. However, triggers can only contain keywords applicable to an inner class.
- In addition, you do not have to manually commit any database changes made by a trigger.
- A trigger invoked by an insert, delete, or update of a recurring event or recurring task results in a runtime error, when the trigger is called in bulk from the Lightning Platform API.
- If Apex trigger completes successfully, than any database changes are automatically committed.
- If Apex trigger does not complete successfully, than any changes made to the database are rolled back.
- For instance suppose that you use an after-insert or after-update trigger to change ownership of leads, contacts, or opportunities. Then if you use the API to change record ownership, or if a Lightning Experience user changes a record’s owner, no email notification is sent. Then to send email notifications to a record’s new owner, set the triggerUserEmail property in DMLOptions to true.
What are different type of Triggers ?
There are two types of triggers:
- Before triggers
- After triggers
Before Triggers :
Before triggers are used to update or validate record values before they’re saved to the database.
After Triggers:
After Triggers 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, such as logging into an audit table or firing asynchronous events with a queue. Therefore, the records that fire the after trigger are read-only.
What is Data Security in Salesforce ?
What are context variables in Triggers ?
All triggers define implicit variables that allow developers to access run-time context.Therefore, these variables are contained in the System.Trigger class.
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 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. 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 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. |
size | The total number of records in a trigger invocation, both old and new. |
Really helpful article. Thanks 🙂
Thanks Ankit
Thank you !! keep visiting for the latest updates
Hello there, simply become aware of your blog via Google, and found that it’s truly informative. I’m going to be careful for brussels. I’ll appreciate for those who continue this in future. Numerous other folks can be benefited from your writing. Cheers!