Hello folks in this blog post we’ll provide you with the brief understanding of Triggers in Salesforce. This blog is targeted to people who are newbies and for Admins who are targeting to learn development.
I’ll start by Introducing myself, I’m Abhishek Chopra working as a Salesforce Developer with 4.5 yrs. Experience, I’m a Platform Developer 1 Certified professional so you can trust me when you read through my blog post. So, let’s gets started.
Let’s start by asking this question…? What is APEX? What is the need to learn Salesforce Development?
- Apex is a Development Platform made by Salesforce, let’s keep it simple and we will expand the definition when you read through this post. So, go on and don’t stop yourself, now is the time to start and don’t be afraid, it’s super easy and fun. Admins, you guys are awesome as you lay the foundation of the Orgs where developers build their customized solution. But what if I tell you if you start getting your hands dirty just a little bit you’ll have the potential to sky rocket in the world of salesforce.
Pre-requisites for this Blog post: – I’m hoping that you are comfortable with the look and feel of the Salesforce platform and have used the point and click features, I’m expecting so I can give you an understanding when and why Apex is important and will dive into one of the most used and important feature Salesforce has i.e. Triggers.
Note: – Before making a jump to apex when you are working and have a scenario in hand, make sure if it can be done via point and click, Never code…! Yes, Never code in such scenarios. We can cover about different point and click tools like (Workflows, Process Builder, Visual Flows) in some other session and explore about it’s wonderful powers.
We will cover the following below topics stated: –
- MVC Pattern
- Types of Trigger
- Write your first salesforce based trigger
MVC Pattern (MODEL – VIEW – CONTROLLER)
Understanding MVC is important because it makes things more scalable and easy to maintain.
- Model: – It represents the database model which includes standard and custom objects.
- View: – The Model is displayed on the view in a nicer format that is visually appealing to the end user and it also accepts the user input.
- Controller: – It represents where all the Application logic resides, It takes the user input from the view and passes onto the Model layer to be updated/inserted/Deleted.
Triggers: – Triggers are apex code that forms the Application logic layer, it runs when an event occurs in the table (Model Layer). This is by far the simplest definition of the triggers.
Types of Triggers: –
- Before Triggers: – Before triggers are used to perform the logic on the same object and specifically we cannot use the DML operation (Insert, update, delete) on these triggers. These triggers fired before the data saved into the database.
- After Triggers: – After triggers are used to perform the logic on the related objects and these are fired when the record gets saved to the database but not committed.
Trigger Events: – Trigger events are the database activities that cause a trigger to run.
Insert, Update, Delete, Undelete
Therefore, in conjunction with the types of triggers we can have the following below Trigger Events.
Trigger Events |
Before Insert |
After Insert |
Before Update |
After Update |
Before Delete |
After Delete |
After Undelete |
Enough of the conceptual talks for now, follow me we’ll write our first Trigger…Here you go.
- We’ll be using Salesforce built in coding environment i.e. Developer Console
(Go to Home -> Click on the dropdown next to your name -> Developer Console )
- Select New -> Apex Trigger
- Specify the Name and the Object on which your Trigger should run.
- On click of Submit you’ll get a blank Trigger boilerplate code as below
- Scenario: – Let’s do a very simple update on the Description field of the Lead Object, when a new Lead is Created or Updated, this Trigger will run.
Add below code to your Trigger and Save.
=============================================================
for(Lead ld : Trigger.New)
{
// This will Update the Lead Description Field on before Insert and before Update Event.
ld.Description = ‘This is an Update from LeadDemoTrigger’;
}
=============================================================
Before testing your trigger lets understand what this loop does and what are context variables.
- The for each loop will Iterate through the records that initiated the Trigger and that record can be accessed using the Trigger.New context variable, so you don’t have to worry about if the action is been performed on the correct record or not.
- The for each loop will run as many number of times as there are records in the Trigger.new context variable list.
- Now you must be thinking why we need a for loop to iterate, the lead record we create and edit will be via UI and there would be one record at a time, but think about times when someone inserts/Updates Lead records via Data Loader, then the loop will Iterate through all the records that are been inserted/Updated via Data loader. Also, let’s say the user updates multiple records via salesforce UI, then it will come as a mass update and all the records will come to trigger in bulk and it has to iterate over each one to process them as per logic defined.
- Always keep in mind that Triggers will always process records in batch of 200 records. Suppose you inserted 500 records via data loader then the trigger will divide 500 records in chunks of 200 records and will run 3 times to complete the action on all the 500 records.
- As I have already stated that Before triggers are used for performing actions on the same object records. The Lead record initiated the Trigger and the same record ‘Description’ field is updated in the run.
- Lastly and the most important thing that you can notice we just mentioned the line, it’s the main logic of Trigger. (we can reference the fields of a record using the dot(.) notation)
ld.Description = ‘This is an Update from LeadDemoTrigger’;
without any DML statement to enforce the change, please note and always keep in mind, we never need DML statements to do any update/changes in record’s field in case of Before Triggers.
- Now let’s test our first Trigger in action.
- Create a new Lead filling in all the mandatory fields and click save. You’ll notice as soon as the record is saved you can see the Description field of the same record is populated with ‘This is an Update from LeadDemoTrigger’ value.
- Same with update action. Take any existing lead, and edit the record and do any changes and save the record, the description field of that record will get updated to ‘This is an Update from LeadDemoTrigger’ value.
Awesome.!! You just have created your first trigger.
excellent information on Triggers and very easy to understand.. Thanks Mr.Abhishek for your article.
Regards
Kamaraj