Apex Triggers In SalesForce

Let's talk briefly about Salesforce.
 
Salesforce is a cloud based online solution for customer relationship management, or CRM. Salesforce provides all of our departments like marketing, sales, commerce, and service with a shared view of our customers with a single integrated CRM platform.
 
"Apex is a strongly typed, object-oriented programming language that allows developers to execute flow and transaction control statements on Salesforce servers in conjunction with calls to the API. Using syntax that looks like Java and acts like database stored procedures, Apex enables developers to add business logic to most system events, including button clicks, related record updates, and Visualforce pages. Apex code can be initiated by Web service requests and from triggers on objects." – Salesforce
 
Apex triggers enable you to perform custom actions before or after changes to Salesforce records, such as insertions, updates, or deletions. A trigger is Apex code that executes before or after the following types of operations: insert, update, delete, merge, upsert, undelete.
 
Trigger Context Variables means all triggers define implicit variables that allow developers to access run-time context. These variables are isExecuting, isInsert, isUpdate, isDelete, isBefore, isAfter, isUndelete, new, newMap, old, oldMap, operationType and size contained in the System.Trigger class.
 
Reading this article, you can learn and test the simple Apex Trigger in SalesForce.
 

Apex Trigger in SalesForce 

 
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
 
Apex Triggers In SalesForce
 
Step 2
 
The General Syntax for Apex Trigger is,
  1. trigger TriggerName on ObjectName (trigger_events) {  
  2.    code_block  
  3. }  
Now, we can create new Apex Trigger HelloTrigger and select the sObject as Account and the file named as HelloTrigger.apxt,
 
Apex Triggers In SalesForce
 
Apex Triggers In SalesForce
 
After creating Apex Trigger HelloTrigger and add the following code for displaying the message before inserting a new account in the Standard Object Account,
  1. trigger HelloTrigger on Account (before insert) {  
  2.    System.debug('Hello World!');  
  3. }  
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,
 
Apex Triggers In SalesForce
  1. Account a = new Account(Name='Hello Trigger');  
  2. insert a;   
Step 4
 
Now we can verify the output. After clicking Execute, Log will open automatically and select "Debug Only" option,
 
Apex Triggers In SalesForce
 
Also in Account sObject added the newly created account name as Hello Trigger
 
Apex Triggers In SalesForce

Summary

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


Similar Articles