Working with Apex Trigger in salesforce

Trigger is piece of code that is executes before and after a record is Inserted / Updated / Deleted from the force.com database.
A trigger is Apex code that executes before or after the following types of actions:

  • Insert
  • Update
  • Delete
  • Merge
  • Upsert
  • Undelete

There are two types of triggers:

  1.     Before triggers: used to update / validate record’s before they are saved to the objects.
  2.     After triggers: used to access fields values that are set by the system (i.e. a record’s Id), and to affect changes in other record’s, such as logging into an audit table or firing asynchronous events with a queue. The record’s that fire the after trigger are read-only.

Additionally, if you update or delete a record in its before trigger, or delete a record in its after trigger, you will receive a runtime error. This includes both direct and indirect operations. For example, if you update account A, and the before update trigger of account A inserts contact B, and the after insert trigger of contact B queries for account A and updates it using the DML update statement or database method, then you are indirectly updating account A in its before trigger, and you will receive a runtime error.

Before creating triggers, consider the following:

  1.     upsert triggers fire both before and after insert or before and after update triggers as appropriate.
  2.     merge triggers fire both before and after delete triggers for the losing records and before update triggers for the winning record only. See Triggers and Merge Statements.
  3.     Triggers that execute after a record has been undeleted only work with specific objects. See Triggers and Recovered Records.
  4.     Field history is not recorded until the end of a trigger. If you query field history in a trigger, you don’t see any history for the current transaction.
  5.     Field history tracking honors the permissions of the current user. If the current user doesn’t have permission to directly edit an object or field, but they activate a trigger that changes an object or field with history tracking enabled, no history of the change is recorded.
  6.     In API version 20.0 and earlier, if a Bulk API request causes a trigger to fire, each chunk of 200 records for the trigger to process is split into chunks of 100 records. In Salesforce API version 21.0 and later, no further splits of API chunks occur. If a Bulk API request causes a trigger to fire multiple times for chunks of 200 records, governor limits are reset between these trigger invocations for the same HTTP request.

Bulk Triggers:
By default every apex trigger is a bulk trigger which is used to process the multiple records at same time as a batch. For each batch of 200 records.

When to use Triggers: 
Let me give you the definitive answer to perhaps the most commonly asked Apex question:
“Should I use a before or after trigger?”

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.

All the trigger context variables are prefixed with “Trigger.” (Ex: Trigger.isInsert, etc..)
isInsert: Returns true if the trigger was fired due to insert operation.
isUpdate: Returns true if the trigger was fired due to update operation.
isDelete: Returns true if the trigger was fired due to delete operation.
isBefore: Returns true if the trigger was fired before record is saved.
isAfter: Returns true if the trigger was fired after record is saved.
New: Returns a list of new version of sObject records.
Old: Returns a list of old version of sObject records.
NewMap: Returns a map of new version of sObject records. (map is stored in the form of map<id,newRecords>)
OldMap: Returns a map of old version of sObject records. (map is stored in the form of map<id,oldRecords>)
Size: Returns a integer (total number of records invoked due to trigger invocation for the both old and new)
isExecuting: Returns true if the current apex code is a trigger.

The below table is tells what are the events we can use in the new trigger and old trigger

Trigger Event Trigger.New Trigger.Old
Before Insert Yes No
Before Update Yes Yes
Before Delete No Yes
Before UnDelete No Yes
After Insert Yes No
After Update Yes Yes
After Delete No Yes

Trigger Context Variable considerations:

  • Trigger.Old is always readOnly
  • We cannot delete trigger.new
  • In before triggers, trigger.new can be used to update the fields on the same object.
  • In After trigger, we get run time exception is thrown when user try to modify the fields in the same object.

Tags:
Apex trigger, soql trigger, salesforce trigger

Leave a Reply

Your email address will not be published. Required fields are marked *