Author - StudySection Post Views - 129 views
Triggers

Salesforce Triggers

A trigger is an Apex script that executes before or after data manipulation language (DML) events occur. Apex triggers enable us to perform custom actions before or after events to record in Salesforce, such as insertions, updates, or deletions.

It is a concept of automating a process and we can solve the complex scenarios that we can’t perform using process builder and workflow. Triggers work with DML operations.

There are two types of triggers –

  1. Before – These are fired before inserting the record in the database.
  2. After – These are fired after inserting the record in the database.

Trigger events in salesforce

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

Context variables in triggers –

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

  • 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 deletes triggers.
  • size: The total number of records in a trigger invocation, both old and new.

When to use salesforce triggers

Triggers should be used in those scenarios where point-and-click tools like workflows don’t work.Workflow is point-and-click which doesn’t need any coding. When you want to take action (email, task, field update or outbound message) for the same object or from Child to parent object, you can use Workflow rules. Whereas Trigger: is a programmatic approach and event driven (insert, update, merge, delete). We can call it an advanced version of Workflow.
Trigger works across all the objects.
We can create a new record through triggers which is not possible through workflow. Also, Workflows work only after some actions but triggers work before and after some actions.

Trigger Syntax

trigger TriggerName on ObjectName (trigger_events) {
code_block
}

Example

This trigger fires before we insert an account.

  1. In the Developer Console, click File | New | Apex Trigger.
  2. Enter HelloWorldTrigger for the trigger name, and then select Account for the sObject. Click Submit.
  3. Replace the default code with the following.
  4. trigger HelloWorldTrigger on Account (before insert) { System.debug(‘Hello World!’); }
  5. To save, press Ctrl+S.
  6. To test the trigger, create an account.
    1. Click Debug | Open Execute Anonymous Window.
    2. In the new window, add the following and then click Execute.
    3. Account a = new Account(Name=’Test Trigger’); insert a;
  7. In the debug log, find the Hello World! statement. The log also shows that the trigger has been executed.

The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.

Leave a Reply

Your email address will not be published.