
Apex triggers enable you to perform custom actions before or afterevents to records in Salesforce, such as insertions, updates, or deletions.
Just like database systems support triggers, Apex provides trigger support for managing records.
Use triggers to perform tasks that can’t be done by using the point-and-click tools in 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.
Triggers can be defined for top-level standard objects, such as Account or Contact, custom objects, and some standard child objects.
Triggers are active by default when created.
Salesforce automatically fires active triggers when the specified database events occur.
Trigger Syntax
The syntax of a trigger definition is different from a class definition’s syntax.
A trigger definition starts with the trigger keyword.
It is then followed by the name of the trigger, the Salesforce object that the trigger is associated with, and the conditions under which it fires.
A trigger has the following syntax:
trigger <TriggerName> on <ObjectName> (trigger_events) {
//code_block
}
To execute a trigger before or after insert, update, delete, and undelete operations, specify multiple trigger events in a comma-separated list.
The events you can specify are:
- before insert
- before update
- before delete
- after insert
- after update
- after delete
- after undelete
Example
This simple trigger fires before you insert an account and writes a message to the debug log.
- In the Developer Console, click File | New | Apex Trigger.
- Enter HelloWorldTrigger for the trigger name, and then select Account for the sObject. Click Submit.
- Replace the default code with the following.
trigger HelloWorldTrigger on Account (before insert) {
System.debug(‘Hello World!’);
}
4. To save, press Ctrl+S.
5. To test the trigger, create an account.
- Click Debug | Open Execute Anonymous Window.
- In the new window, add the following and then click Execute.
Account a = new Account(Name=’Test Trigger’);
insert a;
6. In the debug log, find the Hello World! statement. The log also shows that the trigger has been executed.
Types of Triggers
There are two types of triggers.
- Before triggers are used to update or validate record valuesbefore 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 are read-only.
Context Variables
To access the records that caused the trigger to fire, use context variables.
For example,
Trigger.New contains all the records that were inserted in insert or update triggers.
Trigger.Old provides the old version of sObjects before they were updated in update triggers, or a list of deleted sObjects in delete triggers.
Triggers can fire when one record is inserted, or when many records are inserted in bulk via the API or Apex.
Therefore, context variables, such as Trigger.New, can contain only one record or multiple records.
For example,
trigger HelloWorldTrigger on Account (before insert) {
for(Account a : Trigger.New) {
a.Description = ‘New description’;
}
}
Trigger Context Variables
isInsertReturns true if this trigger was fired due to an insert operation, from the Salesforce user interface, Apex, or the API.
isUpdateReturns true if this trigger was fired due to an update operation, from the Salesforce user interface, Apex, or the API.
isDeleteReturns true if this trigger was fired due to a delete operation, from the Salesforce user interface, Apex, or the API.
isBeforeReturns true if this trigger was fired before any record was saved.
isAfterReturns true if this trigger was fired after all records were saved.
isUndeleteReturns true if this trigger was fired after a record is recovered from the Recycle Bin.
newReturns a list of the new versions of the sObject records.
newMap A map of IDs to the new versions of the sObject records.
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.
No comments:
Post a Comment