Triggers in Salesforce | Types of Triggers and Syntax

1. Triggers in Salesforce Tutorial

Today, in this Triggers in Salesforce tutorial, we are going to learn what is Triggers in Salesforce. Moreover, we will discuss the Apex Trigger, Types of Triggers in Salesforce. Also, we will see Trigger Syntax and Trigger Context Variables in Salesforce. At last, we will learn Salesforce Trigger Example.

So, let’s start Triggers in Salesforce for beginners.

Triggers in Salesforce | Types of Triggers and Syntax

Triggers in Salesforce | Types of Triggers and Syntax

2. Apex Triggers in Salesforce

Apex often invokes by using triggers. The function Apex triggers avail you the freedom to perform custom actions before making changes to Salesforce records, like insertions, updates, or deletions.

Have a look at Salesforce Lightning 

A trigger is the Apex code that executes before or after the subsequent kinds of operations:

  • insert
  • update
  • merge
  • upsert
  • undelete
  • delete

Example of Triggers in Salesforce, you’ll have a trigger run before an object’s records are inserted into the database, once records are deleted, or perhaps after a record is fixed from the recycle bin.
To outline a trigger, from the object management settings for the object whose triggers you wish to access, attend Triggers.

Do you know about Salesforce Data Security Model

3. Types of Triggers in Salesforce

After triggers are used to access field values that are set by the system (such as a record’s Id or LastModifiedDate field), and to have an effect on changes in alternative records, like logging into an audit table or firing asynchronous events with a queue. The records that fire the once trigger are read-only.
Saleforce Triggers may modify alternative records of identical type because the records that originally fired the trigger. As a Salesforce Trigger Example, if a trigger fires after an update of contact A, the trigger may modify contacts B, C, and D. Hence, Triggers in Salesforce will cause alternative records to change, and since these changes will, in turn, fire additional triggers. The Apex runtime engine considers all such operations one unit of work and sets limits on the amount of operations which will perform to stop the infinite recursion. 

Let’s revise the Salesforce Sharing Rules

Additionally, if you update or delete a record in its before trigger, or delete a record in its after trigger, you may receive a runtime error. This includes each direct and indirect operations. For example, if you update account A, and therefore the before update trigger of account A inserts contact B, and therefore the after insert trigger of contact B queries for account A. Updates it mistreatment the DML update statement or database technique, then you’re indirectly updating account A in its before trigger, and you may receive a runtime error.

4. Implementing the Triggers in Salesforce

  • Upsert triggers fire each before and once insert or before and after update triggers as applicable.
  • Merge triggers fire each before and after delete for the losing records, and both before and after update triggers for the winning record. 
  • Triggers that execute once a record has been undeleted only work with specific objects. 
  • Field history not records till the top of a trigger.

Do you know about Top Salesforce Terminologies

  • Field history tracking provides the liberty or permissions of the current user. If this user doesn’t have permission to directly edit an object or field. However, the user activates a trigger that changes an object or field with history tracking enabled, no history of the change record.
  • Callouts should be created asynchronously from a trigger so that the trigger method isn’t blocked while looking ahead to the external service’s response. The asynchronous callout is created in an exceeding background process, and also the response is received once the external service returns it. To form an asynchronous callout, use asynchronous Apex such as a future method.

5. Trigger Context Variable in Salesforce

a. isExecuting- Genuine if the current setting for the function Apex code is a trigger, not just a Visualforce page, a web benefit, or an execute anonymous() API call.
b. isInsert- Genuine if this trigger was eliminated because of an embed activity, from the Salesforce UI, Apex, or the API.
c. isUpdate- Returns genuine if this trigger was let go because of a refresh task, from the Salesforce UI, Apex, or the API.
d. isDelete- Genuine if this trigger lets go due to the reason of an erase activity, from the Salesforce user interface, Apex, or the API.
e. isBefore- Returns genuine if this trigger terminates before any record was spared.

Let’s learn about Salesforce List Controller

f. isAfter Returns genuine if this trigger was terminated after all records were spared.
g. isUndelete- Genuine if this trigger was let go once just after a record is recouped from the Recycle Bin (that is after an undelete task from the Salesforce UI, Apex, or the API.)
h. new- Returns a rundown of the new forms of the sObject records. This sObject list is just accessible in the embed, refresh, and undelete triggers, and the records must be changed in before triggers.
i. newMap- A directory of IDs to the new kind of the sObject records. This guide is just accessible in before refresh, after embed, after refresh, and after undelete triggers.
j. Old- Returns a rundown of the old forms of the sObject records. This sObject list is just accessible in refresh and erases triggers.
k. oldMap- A guide of IDs to the old renditions of the sObject records. This guide is just accessible in refresh and erases triggers.
l. Size- The add up to the number of records in a trigger summon, both old and new.

Do you know about Salesforce Automation

For Example:

Trigger simpleTrigger on Account (after insert) {
   for (Account a : Trigger.new) {
       // Iterate over each sObject
   }
   // This single query finds every contact that is associated with any of the
   // triggering accounts. Note that although Trigger.new is a collection of
   // records, when used as a bind variable in a SOQL query, Apex automatically
   // transforms the list of records into a list of corresponding Ids.
   Contact[] cons = [SELECT LastName FROM Contact
                     WHERE AccountId IN :Trigger.new];
}
Salesforce Quiz

6. Trigger Syntax

The language structure of a trigger definition is not the same as a class definition’s punctuation. A trigger definition begins with the trigger catchphrase. Then it trails by the name of the trigger, the Salesforce protest that the trigger relates with, and the conditions under which it fires.

trigger TriggerName on ObjectName (trigger_events) {
  code_block
}

To execute a Trigger in Salesforce previously or after embed, refresh, erase, and undelete tasks, indicate various trigger occasions in a comma-isolated rundown. The occasions you can indicate are:

Have a look at Salesforce Careers in India

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

Illustration
This basic Salesforce Trigger flames previously you embed a record and composes a message to the investigate log.

  • In the Developer Console, click File | New | Apex Trigger.
  • Enter HelloWorldTrigger for the trigger name, and after that select Account for the sObject. Snap Submit.
  • Supplant the default code with the accompanying.
trigger HelloWorldTrigger on Account (before insert) {
System.debug('Hello World!');
}
  • To spare, press Ctrl+S.
  • To test the trigger, make a record.

Let’s revise SOQL vs SOSL Salesforce

  1. Snap Debug | Open Execute Anonymous Window.
  2. In the new window, including the accompanying and after that snap Execute.
  • In the investigate log, discover the Hello World! explanation. The log additionally demonstrates that the trigger has been executed.

So, this was all in Triggers in Salesforce. Hope you like our explanation.

7. Conclusion: Triggers in Salesforce

Hence, in this Salesforce Triggers tutorial, we learned what is triggers in Salesforce. Moreover, we discussed types of triggers and Salesforce triggers example. Also, we saw Trigger Context Variable, Apex Trigger, and Trigger Syntax. Still, if you have any query, feel free to ask in the comment tab.

See also – 
Static resource in Salesforce

For reference

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.