triggers in salesforce

Triggers in Salesforce - The Complete Guide

Triggers in Salesforce

A trigger is an Apex content that executes previously or after information control language (DML) occasions happen. Pinnacle triggers empower you to perform custom activities previously or after occasions to record in Salesforce, for example, inclusions, updates, or erasures. Much the same as database frameworks bolster triggers, Apex offers trigger help for overseeing records.

Trigger Syntax

trigger TriggerName on ObjectName (trigger_events) {
}

A trigger is a lot of the explanation which can be executed on the accompanying occasions. In the above trigger occasions, at least one of the beneath occasions can be utilized with comma-isolated. Different occasions on which a trigger can fire.

Here is a list of events in Salesforce which can fire trigger:

  • before insert
  • before update
  • before delete
  • after insert
  • after update
  • after delete
  • after undelete   

dont miss out iconDon't forget to check out: Trigger Assignment Rules on Update or Upsert using Data Loader | Salesforce Tutorials

All triggers characterize understood factors that permit designers to get to the run-time settings. These factors are contained in the System Trigger class

Here is the once-over of setting factors in triggers: 

  • isExecuting: Returns legitimate if the current setting for the Apex code is a trigger, not a Visualforce page, a Web organization, or an executeanonymous() API call. 
  • isInsert: Returns legitimate if this trigger was ended on account of an expansion action, from the Salesforce UI, Apex, or the API
  • isUpdate: Returns substantial if this trigger was ended as a result of an update action, from the Salesforce UI, Apex, or the API. 
  • isDelete: Returns legitimate if this trigger was ended on account of a delete action, from the Salesforce UI, Apex, or the API. 
  • isBefore: Returns substantial if this trigger was ended before any record was saved. 
  • isAfter: Returns substantial if this trigger was ended after all records were saved. 
  • isUndelete: Returns substantial if this trigger was ended after a record is recovered from the Recycle Bin (that is, after an undelete movement from the Salesforce UI, Apex, or the API.) 
  • new: Returns a summary of the new types of the sObject records. This sObject list is only available in supplement, update, and undelete triggers, and the records must be modified before triggers. 
  • newMap: A guide of IDs to the new types of the sObject records. This guide is only open in before update, after enhancement, after update, and after undelete triggers. 
  • old: Returns a once-over of the old interpretations of the sObject records. This sObject list is only open in invigorate and eradicate triggers. 
  • oldMap: A guide of IDs to the old adjustments of the sObject records. This guide is only open in revive and delete triggers. 
  • size: The hard and fast number of records in a trigger conjuring, both old and new.

Different type of Triggers

There are two types of triggers:

  • Before triggers are utilized to play out an errand before a record is embedded or refreshed or erased. These are utilized to refresh or approve record esteems before they are spared to the database
  • After triggers are utilized on the off chance that we need to utilize the data set by the Salesforce framework and to make changes in different records. are utilized to get to handle esteems that are set by the framework, (for example, a record's Id or LastModifiedDate field), and to influence changes in different records. The records that fire after a trigger is perused as it were.

Recursive Trigger and how to avoid it:

There is a possibility that the result of the trigger can end up calling the same trigger again and can run in a loop, this is known as a recursive trigger. To avoid this scenario we should create a static variable and check the value of this variable before we execute anything in the trigger.

dont miss out iconCheck out another amazing blog by Sumit here: Email Services in Salesforce with Simple Example

Before creating triggers, consider the following:

  • Upsert triggers to fire both when embedding or when update triggers as proper. 
  • Mix triggers fire both when delete triggers for the losing records and before update triggers for the triumphant record figuratively speaking. See Triggers and Merge Statements
  • Triggers that execute after a record has been undeleted simply work with express articles. See Triggers and Recovered Records. 
  • Field history isn't recorded until the completion of a trigger. If you question field history in a trigger, you don't see any history for the current trade.

Field history following distinctions the consents of the current client. In the event that the current client doesn't have authorization to legitimately alter an article or field, yet they initiate a trigger that changes an item or field with history following empowered, no history of the change is recorded. 

In API structure 20.0 and earlier, if a Bulk API request makes a trigger fire, each chunk of 200 records for the trigger to process is part into bits of 100 records. In Salesforce API interpretation 21.0 and later, no further pieces of API knots occur. On the off chance that a Bulk API demand makes a trigger fire on various occasions for lumps of 200 records, representative cutoff points are reset between these trigger summons for a similar HTTP demand.

References: data-flair.training, salesforce.stackexchange, adhiman.com, trailhead.salesforce.com"

Popular Salesforce Blogs