Apex Trigger in Salesforce

Apex triggers enable you to perform custom actions before or after events to records in Salesforce such as insert, update or delete.Apex triggers enable you to perform custom actions before or after events to records in Salesforce such as insert, update or delete.
Type of Triggers – There are 2 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 LastModifiedDatefield), and to affect changes in other records. The records that fire the after trigger are read-only.

Trigger 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. You can iterate over Trigger.New to get each individual sObject.

NOTE- The system saves the records that fired the before trigger after the trigger finishes execution. You can modify the records in the trigger without explicitly calling a DML insert or update operation. If you perform DML statements on those records, you get an error.

Variable Usage
isExecuting Returns true if the current context for the Apex code is 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.
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 undeletetriggers.

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.

Using Trigger Exception:- Use addError() method to handle error inside trigger. The error message is displayed in the UX and is logged.
Calling addError() in a trigger causes the entire set of operations to roll back, except when bulk DML is called with partial success.
Users are not allowed to delete an Account if they have related Opportunities

trigger AccountTrigger on Account(before delete) {
//Error on deletion of Account having related opportunities
for(Account acc :[SELECT Id FROM Account where Id IN (Select AccountId FROM                                                                        Opportunity) AND Id IN : trigger.old]) {
Trigger.oldmap.getId(acc.Id).addError(Cannot delete account with related                                                                                                     opportunities);
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s