Select Page
February 13, 2020

What Is Trigger In Salesforce?

Salesforce has been dominating the business CRM market since it was introduced more than two decades ago. It has sufficiently helped business enterprises in making their business processes easier by providing automation and efficiency in handling customer database. There are several services offered by Salesforce CRM – be it lead management, email marketing, advertising or carrying out productive integrations.

A Salesforce Development Company would provide services that are specifically tailored to the needs of the client businesses. Multiple developers are employed that take care of every aspect of the platform and provide specific services. In order to understand the manner in which data is handled using the platform of Salesforce CRM, it is important to scan through the concept of Apex in Salesforce.

What is Apex in Salesforce?

Apex is an important code pertaining to a programming language that is multi-tenant and on-demand for Salesforce Developers. This language is highly preferred by developers that are interested in building highly enhanced applications for client businesses. The platform of Apex in Salesforce has brought in a considerable change in the manner in which applications are built and managed.

The combination of Apex and the platform of Salesforce CRM provide the users with several effective customisations. Majority of these customisations allow developers in defining new workflows, fields, objects and approval processes. Also, Apex in Salesforce allows the developers to use SOAP API for issuing commands for data manipulation like delete( ), upsert( ) and update( ).

The above mentioned commands are issued from client-side programs that are generally written in JavaScript, Java or .NET. They provide the client organisations with high flexibility in carrying out the necessary customisations. Using Apex on the platform of Salesforce provides the developers with stronger CRM functionality due to the pre-built Salesforce applications. Moreover, Salesforce also allows developers to customise these pre-built applications to suit the needs and requirements of the client organisation.

However, there may be certain business processes that do not fall under the purview of the concerned functionality. In such cases, Apex in Salesforce Lightning allows the advanced administrators in building custom functionality according to the necessary requirements.

Also, Apex functions completely on an on-demand basis on the platform of Salesforce Lightning. Developers can write and save the Apex code on the Lightning Platform, following which the users can trigger the functionality of the code used through the concerned user interface.

In order to successfully invoke Apex to perform highly customised activities, developers often take help of specific Apex triggers in Salesforce.

What is Trigger in Salesforce?

As mentioned above, triggers are the components used to evoke Apex in Salesforce that allows developers to undertake specific and customised actions, before or after changes are made in Salesforce records. These changes may be regarding updating, deleting or inserting necessary data into the system.

Trigger in Salesforce is essentially an Apex script used by developers before or after events related to data manipulation language (DML). Here are some of the key types of operations executed by a trigger:

  • Inserting data into the system
  • Updating data within the system
  • Deleting data from the system
  • Merging data across platforms
  • Upserting data into the system
  • Undeleting data from the system

A good example of developers using triggers in Salesforce can be that of running a trigger before or after records are restored into Recycle Bin. Triggers can be defined for objects belonging to the top-level standards. These are the objects that support triggers, such as Contacts or Accounts, pertaining to a database. Triggers can also be defined for certain standard child objects like CaseComment and other relevant custom objects.

You can define triggers in Salesforce by choosing the option of “Triggers” for the object whose triggers you want to access from the option of “Object Management Settings”.

There are broadly two types of triggers that can be run by developers:

  • Before Triggers – These are the triggers run for updating or validating necessary record values before they are saved to the platform database.
  • After Triggers – These are the triggers that are read-only in nature and allow developers to access the field values set by the system and influence changes occurring in different records (such as firing asynchronous events using a queue or logging into an audit table).

Why Is Running Triggers Important?

It is important for Salesforce Developers to run certain specific triggers as they help the users in modifying other records belonging to the same type as that of the records that initiated the trigger. As triggers can cause this modification of records and can also lead to firing of more triggers, the runtime engine of Apex sets certain limits on the number of operations a user can perform for preventing infinite recursion.

Bulk Triggers

Bulk triggers refer to the triggers that allow the developers to process several records at the same time. By default, all the triggers run by Salesforce Developers are bulk triggers and are able to handle single record updates and bulk operations as follows:

  • Performing mass actions, such as changes in the owner record
  • Importing necessary data into the system
  • Making bulk API calls
  • Employing recursive Apex methods and triggers to invoke bulk DML statements

What Are Trigger Context Variables?

Trigger Context Variables are the definite variables contained in the system that are used by developers to access run-time context. Here are some of the key variables and their uses:

  • isExecuting – This is the variable that returns true in case the present context of the employed Apex code is not a Visualforce page, an executeanonymous( ) API call or a web service, but a defined trigger.
  • isInsert – This is the variable that returns true in case the concerned trigger was fired because of an insert operation.
  • isUpdate – This variable returns true in case the fired trigger was due to an operation pertaining to an update.
  • isDelete – This variable returns true in case a trigger was fired because of a delete operation.
  • isBefore – This variable returns true in case a trigger was fired before the system saved any record.
  • isAfter – This variable returns true if a trigger was fired after all the records were saved in the system.
  • isUndelete – This variable returns true when a record is extracted out of the recycle bin.
  • new – This is the variable that returns a list containing new versions of the records holding Salesforce objects.
  • newMap – This is the variable that acts as a map of all relevant Ids to newer versions of the records containing Salesforce objects. You can only avail this variable in before update, after update, after insert and after undelete triggers.
  • old – This is the variable that returns a list containing the old versions of Salesforce object records. This variable can only be availed in delete and update triggers.
  • oldMap – This is the variable that serves as a map of all relevant Ids of older records containing Salesforce objects. This variable can only be availed in delete and update triggers.
  • size – This is the variable that shows the totality of records (old and new) when a trigger is invoked.

Actions Performed By Trigger Events

As mentioned above, developers use different trigger events in Salesforce to undertake specific operations pertaining to data manipulation language. Here are some of the key actions performed by those trigger events:

  • beforeinsert – This event performs the activity of changing fields through the platform of trigger.new.
  • afterinsert – This event can indulge in updating an original Salesforce object with the help of an “update DML” operation.
  • beforeupdate – This event is only limited to changing fields using trigger.new.
  • afterupdate – This event is capable of updating an original object through an “update DML” operation. It can also delete an original object with the help of a “delete DML” operation.
  • beforedelete – This event can only update an original object with the help of an “update DML” operation.
  • afterdelete – This trigger is not able to perform majority of the actions as trigger.new is not available for the after delete triggers. Also, it cannot be used for updating objects as the object is already deleted.
  • afterundelete – This event cannot change fields with the help of trigger.new as doing the same would show a runtime error and trigger.new cannot be availed by undelete triggers. However, it can be used for updating an original object using an “update DML” operation. It can also be used for deleting the original object with the help of “delete DML” operation but it would be unnecessary to do so, owing to the object being immediately deleted as it is inserted into the system.

The use of triggers is critically important for Salesforce Developers as they help them manage sensitive customer data within the system, specifically according to the requirements of the client businesses. However, it is advisable for developers to ensure precision while running triggers, as a little mishap can have dire implications.

Schedule Meeting



Leave a Reply

avatar
  Subscribe  
Notify of
Related Posts
How To Create Salesforce Reports?

How To Create Salesforce Reports?

No matter which industry sector your business belongs to and what your scale of your operations is, your business will...