What is Salesforce trigger?
Apex triggers enable you to perform custom actions before or after events to records in Salesforce, such as insertions, updates, or deletions. Just like database systems support triggers, Apex provides trigger support for managing records.
When to use salesforce triggers
you use triggers to perform operations based on specific conditions, to modify related records, or restrict certain operations from happening.
What is Trigger Syntax?
trigger TriggerName on ObjectName (trigger_events) {
code_block
}
Trigger events in salesforce?
A trigger is a set of statements that can be executed on the following events. In the above trigger events, one or more of the below events can be used with comma-separated.
Here is a list of trigger events in salesforce:
before insert
before update
before delete
after insert
after update
after delete
after undelete
What are different type of Triggers?
There are two types of 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 LastModifiedDate field) and to affect changes in other records. The records that fire the after trigger is read-only.
What are context variables in triggers?
All triggers define implicit variables that allow developers to access runtime 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.
Salesforce Trigger Example
trigger AccountControllerTrigger on Account (before insert, before update) {
for(Account acc : Trigger.New) {
if(acc.Industry != null && (acc.Industry == ‘Banking’ || acc.Industry == ‘Healthcare’)){
acc.Rating = ‘Hot’;
}
}
}