Apex Trigger
is an apex script that executes on a specific event. There are the following events:
- Insert
- Update
- Delete
- Undelete
- Upsert
- Merge
Types of triggers
Before triggers
- Before-trigger events occur before a record’s changes are committed to the database.
- This particular event is ideal for performing data validation, setting default values, or performing additional logic and/or calculations.
After triggers
- After-trigger events occur after a record has been committed to the database, which means that records being inserted will have a record id available.
- This particular event is ideal for working with data that is external to the record itself such as referenced objects or creating records based on information from the triggered object. Suppose need to insert the contact whenever an Account is inserted.
Trigger Context Variables
isExecuting:
- A better name for this flag might be "isCreatedByTrigger".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.When decoupling the Apex trigger handler class from the actual trigger, the Apex class has no way to know what context it's called in (unit test, web service, visualforce page, trigger). This flag just means the handler was executed by a trigger.
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.
Trigger.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.
Trigger.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.
Trigger.newMap
- A map of IDs to the new versions of the sObject records.
Trigger.oldMap
- A map of IDs to the old versions of the sObject records.
- We cannot write the trigger on object where istriggerable is false like AccountContractRole
Best Practices of writing the trigger:
While
trigger the trigger, we should keep following points in mind:
- Try to write one trigger on one object
- Use handler class to write the logic
- Use context variable to filter the records
- Use Map and Set in the trigger
- Minimize the number of SOQL statements by preprocessing records and generating sets, which can be placed in single SOQL statement used with the IN clause.
- Minimize the number of data manipulation language (DML) operations by adding records to collections and performing DML operations against these collection
No comments:
Post a Comment