What is a Salesforce trigger

By: Prem Tiwari | Last Updated: | In: Salesforce


A Salesforce trigger is written in Apex programming. By using the Apex trigger, you can to perform custom actions like insertions, updates, or deletions before and after changes to Salesforce records in your Org.

You can perform the following types of operations using Apex trigger:

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

Below is the basic syntax of Apex Trigger:

Here is another example of a trigger. Below trigger will execute before insert & before the update to Salesforce records.

Trigger Context Variables

VariableUsage
isExecutingReturns true if the current context for the Apex code is a trigger,
not a Visualforce
page, a Web service, or an executeanonymous() API call.
isInsertReturns true if this trigger
was fired due to an insert operation, from the Salesforce user
interface, Apex, or the API.
isUpdateReturns true if this trigger
was fired due to an update operation, from the Salesforce user
interface, Apex, or the API.
isDeleteReturns true if this trigger
was fired due to a delete operation, from the Salesforce user
interface, Apex, or the API.
isBeforeReturns true if this trigger
was fired before any record was saved.
isAfterReturns true if this trigger
was fired after all records were saved.
isUndeleteReturns 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
delete triggers.

sizeThe total number of records in a trigger invocation, both old and
new.

Reference

Share this post!


About: Prem Tiwari

Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies.