Apex Triggers In SalesForce


Let’s briefly delve into Salesforce.

Salesforce stands as a cloud-based online solution for Customer Relationship Management, commonly known as CRM. It furnishes our various departments, including marketing, sales, commerce, and service, with a unified perspective on our customers through an integrated CRM platform.

“Apex is a strongly typed, object-oriented programming language that empowers developers to execute flow and transaction control statements on Salesforce servers, coupled with calls to the API. Resembling Java in syntax and acting akin to database stored procedures, Apex enables developers to incorporate business logic into various system events, encompassing button clicks, related record updates, and Visualforce pages. Apex code can be triggered by Web service requests and through triggers on objects.” – Salesforce

Apex triggers facilitate the execution of custom actions before or after modifications to Salesforce records, such as insertions, updates, or deletions. A trigger, essentially Apex code, operates before or after specific types of operations: insert, update, delete, merge, upsert, undelete.

Trigger Context Variables refer to implicit variables defined by all triggers, enabling developers to access runtime context. These variables, found in the System.Trigger class, include isExecuting, isInsert, isUpdate, isDelete, isBefore, isAfter, isUndelete, new, newMap, old, oldMap, operationType, and size.

By perusing this article, you can gain insights and experiment with the basics of Apex Triggers in Salesforce.

Salesforce Apex Trigger

The prerequisites for testing Apex Trigger in SalesForce are:

R· Register a Salesforce Free Trail account using the following link.

Step 1

Login your Salesforce Account and Click the Developer Console

Step 2

The General Syntax for Apex Trigger is,

trigger TriggerName on ObjectName (trigger_events) {  
   code_block  
}  

Now, we can create new Apex Trigger HelloTrigger and select the sObject as Account and the file named as HelloTrigger.apxt,

After creating Apex Trigger HelloTrigger and add the following code for displaying the message before inserting a new account in the Standard Object Account,

trigger HelloTrigger on Account (before insert) {  
   System.debug('Hello World!');  
}  

Step 3

For Debugging the Apex Trigger HelloTrigger,

Click Debug menu and Select Open Execute Anonymous Window,

Apex Triggers In SalesForce

Now, write the Apex code for Trigger, and enable the Open log option for Viewing output and click Execute,

Account a = new Account(Name='Hello Trigger');  
insert a; 

Step 4

 Now we can verify the output. After clicking Execute, Log will open automatically and select “Debug Only” option,

Also in Account sObject added the newly created account name as Hello Trigger

Apex Triggers In SalesForce

Overview

Now you have successfully tested the Apex Trigger in a SalesForce environment.