Trigger Context Variables
All triggers define implicit variables that allow developers to access run-time 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.
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. |
For example, in this simple trigger,
Trigger.new is a list of sObjects and can be iterated over in a
for loop, or used as a bind variable in the
IN clause of a SOQL query.
01 | Trigger simpleTrigger on Account (after insert ) { |
02 | for (Account a : Trigger. new ) { |
10 | Contact[] cons = [ SELECT LastName FROM Contact |
11 | WHERE AccountId IN :Trigger. new ]; |
This trigger uses Boolean context variables like
Trigger.isBefore and
Trigger.isDelete to define code that only executes for specific trigger conditions:
01 | trigger myAccountTrigger on Account(before delete, before insert , before update , |
02 | after delete, after insert , after update ) { |
03 | if (Trigger.isBefore) { |
04 | if (Trigger.isDelete) { |
08 | for (Account a : Trigger.old) { |
09 | if (a.name != 'okToDelete' ) { |
10 | a.addError( 'You can\'t delete this record!' ); |
17 | for (Account a : Trigger. new ) { |
18 | if (a.name == 'bad' ) { |
19 | a.name.addError( 'Bad name' ); |
22 | if (Trigger.isInsert) { |
23 | for (Account a : Trigger. new ) { |
24 | System .assertEquals( 'xxx' , a.accountNumber); |
25 | System .assertEquals( 'industry' , a.industry); |
26 | System .assertEquals( 100 , a.numberofemployees); |
27 | System .assertEquals( 100.0 , a.annualrevenue); |
28 | a.accountNumber = 'yyy' ; |
33 | if (Trigger.isInsert) { |
34 | List <Contact> contacts = new List <Contact>(); |
35 | for (Account a : Trigger. new ) { |
36 | if (a.Name == 'makeContact' ) { |
37 | contacts.add( new Contact (LastName = a.Name, |
Context Variable Considerations
Be aware of the following considerations for trigger context variables:
- trigger.new and trigger.old cannot be used in Apex DML operations.
- You can use an object to change its own field values using trigger.new, but only in before triggers. In all after triggers, trigger.new is not saved, so a runtime exception is thrown.
- trigger.old is always read-only.
- You cannot delete trigger.new.
The following table lists considerations about certain actions in different trigger events:
before insert | Allowed. | Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. | Not applicable. The original object has not been created; nothing can reference it, so nothing can update it. |
after insert | Not allowed. A runtime error is thrown, as trigger.new is already saved. | Allowed. | Allowed, but unnecessary. The object is deleted immediately after being inserted. |
before update | Allowed. | Not allowed. A runtime error is thrown. | Not allowed. A runtime error is thrown. |
after update | Not allowed. A runtime error is thrown, as trigger.new is already saved. | Allowed. Even though bad code could cause an infinite recursion doing this incorrectly, the error would be found by the governor limits. | Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. |
before delete | Not allowed. A runtime error is thrown. trigger.new is not available in before delete triggers. | Allowed. The updates are saved before the object is deleted, so if the object is undeleted, the updates become visible. | Not allowed. A runtime error is thrown. The deletion is already in progress. |
after delete | Not allowed. A runtime error is thrown. trigger.new is not available in after delete triggers. | Not applicable. The object has already been deleted. | Not applicable. The object has already been deleted. |
after undelete | Not allowed. A runtime error is thrown. | Allowed. | Allowed, but unnecessary. The object is deleted immediately after being inserted. |
No comments:
Post a Comment