Call Us Today !

How to use a trigger in Salesforce

You may use Apex triggers to perform custom actions in Salesforce. These actions can be performed before or after events. Simply put, they help in records management.

You will basically use triggers when point and click tools on the interface can’t perform the tasks that you need to do. They are typically used to perform operations given certain conditions. They are also used to restrict operations and to modify records.

Types of Triggers

There are two types of triggers in Salesforce. There are Before Triggers and the After Triggers.
Before Triggers: These triggers are used to update or validate the values of records before they are saved to a database.

After Triggers: The records accessed by After Triggers are read only. These triggers access field values. These values are set by the system. They are also used to make changes in other records as well.

Trigger Syntax

A trigger has the following syntax:

trigger TriggerName on ObjectName (trigger_events) {

The definition of a trigger begins with the keyword “trigger.” It is then followed by the name of that trigger followed by the Salesforce object that is associated with it.

As stated earlier, there are two kinds of triggers – before and after. That means you can execute a trigger before or after operations such as delete, undelete, and insert. The following are events that you can specify:

  • After delete
  • After undelete
  • After insert
  • After update
  • Before update
  • Before delete
  • Before insert

You can make a trigger run after a record is restored from the Recycle Bin, after records have been deleted, before records are inserted into the database etc.

There are certain considerations before you create a trigger. For instance, remember that upsert triggers will fire both before and after insert and update triggers.

Merge triggers also fire before and after delete and update triggers. You should also make callouts from a trigger asynchronously.

Share this article!

Partager sur facebook
Partager sur twitter
Partager sur linkedin
Partager sur email