If you are looking to build a powerful web or mobile application then Apex, Salesforce’s proprietary platform language, would be an ideal solution. 

Salesforce, one of the powerful and flexible CRM solution providers, offers the most powerful Force.com programming language known as Apex to help you create powerful business applications.

Apex in Salesforce

Apex is an object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce.com.

Apex Trigger in Salesforce is an Apex code that gets executed whenever a specific DML operation is performed on an Object record. It enables developers to programmatically check for duplicate records, update fields across objects, automate the creation of new records based on criteria, etc.

A trigger is an Apex script that executes before or after specific data manipulation language (DML) events occur, such as before object records are inserted into the database, or after records have been deleted.

A trigger is an Apex code that executes before or after the following types of operations like insert, update, and delete.

Two Types of Triggers

  • Before Triggers-Before triggers are used to execute a task before a record is inserted or updated or deleted. These triggers are used to update or validate record values before they are saved to the Salesforce database.
  • After Triggers-They are used to access values of the record that are stored in the database and use this value to make changes with other records. They get executed after the record is saved to the Salesforce database.

Bulky Trigger

By default, all triggers in Salesforce are bulky triggers. Bulky triggers define that you can process multiple records at a time. Bulky triggers can handle bulk operations and single-record updates such as:

  • Importing data
  • Mass actions
  • Bulk API calls
  • Recursive Apex methods and triggers call bulk DML statements

Trigger Context Variables in Salesforce

isExecuting: Returns true, if the current apex code is triggered. 

isBefore: Returns true if the code inside trigger context is executed before the record is saved.

isAfter:  Returns true if the code inside trigger context is executed after the record is saved.

isInsert: Returns true if the code inside trigger context is executed due to an insert operation.

isUpdate: Returns true if the code inside trigger context is executed due to an update operation.

isDelete: Returns true if the code inside trigger context is executed due to delete operation.

isUnDelete: Returns true if the code inside trigger context is executed due to undelete operation, i.e., when we recovered data from the recycle bin.

new: Returns the new version of the object records. If inserted/updated 10 records, trigger.new will contain 10 records.

newMap: Returns a map of a new version of sObject that contains an ID’s as a key and the old versions of the sObject records as value. This map is only available in before update, after insert, and after update triggers.

old: Returns the old version of the object records.

oldMap: Returns a map of the old version of the sObject that contains an IDs as a key and the new versions of the sObject records as value. This map is available for the only update and delete triggers.

size: Returns the size of the manipulated record. It will return one if you insert one record. It will return the size of the record you are inserting, updating, deleting, or undeleting.

Factors to consider when implementing Salesforce Triggers

Merge triggers are fired on both (insert and update) events on delete operation.

  • Upset triggers are fired on four different events such as before (insert and update) and after (insert and update).
  • Field history is updated after the trigger has successfully executed data and information processing.
  • A trigger cannot have a static keyword within its code.
  • Any callout should be asynchronous so that the trigger does not have to wait for the response.
  • If a trigger executes successfully then the changes are committed to the database and if it fails, the transaction is rolled back.

Syntax of Salesforce Trigger 

trigger triggerName on ObjectName (trigger_events) {

    //code_block

} trigger TestTrigger on Case (Before Insert,After Insert,Before Update ,After Update ,Before Delete ,After Delete,After Undelete) {

  //Code Block

 }

  1. Apex trigger always begins with a keyword trigger.
  2. Enter a Trigger name.
  3. Enter the condition.
  4. To execute trigger on a case like before insert, after insert, before the update, after the update, before delete, after delete, after undelete, you must specify the trigger event.

Conclusion

Salesforce Trigger is an Apex code that gets executed before or after inserting or modifying a record based on the specified condition. 

Triggers in Salesforce can be defined as the programmatic event handlers that get executed when a record is saved. They get executed when a DML operation occurs on a sObject record. Salesforce Triggers can handle Database manipulation language (DML) operations, execute Salesforce Object Query Language (SOQL), and call custom Apex methods. 

If you are looking to explore more about Salesforce triggers, feel free to reach out to certified Salesforce experts and specialists at Cloud Analogy, the world’s leading Salesforce Consulting Partner. Cloud Analogy offers a wide range of cost-effective Salesforce consulting services in UK including admin support, development, data migration, customization, and more. Choose the world’s leading Salesforce Consulting Company now!

Pin It on Pinterest