 Hello friends. I am Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. This is my website studiesalesforce.com. So here you can find links of various videos related to Salesforce. In this video, I'm going to discuss about Apex Trigger in Salesforce. So first of all, introduction. So first point says we can perform custom operations before or after events to record. So trigger provides two events. One is before and another one is after, right? So you can use triggers to do operations that cannot be done by point and click tools provided in Salesforce. So if I take example of point and click tools, those are like workflow rules, process builders and flows. So if you can't do any operation through those point and click tools. So in that case, you can use triggers. So in triggers, you need to write the custom code with the help of Apex. So in that way, you can use these to perform operations in before event and after event. So third point says we can do things using triggers that we can do through Apex, including execution, SOQL and DML or calling custom methods. So with the help of Apex, we can do these operations through triggers. So SOQL, DML and calling of custom methods. So these operations we can do. Then triggers can be created for both standard and custom objects and by default, triggers are active as you create them. So later on, you can deactivate them as well. But as you create any trigger, so by default, it is activated. So later on in this video, I will show you like how we can create triggers with the help of demonstration. So first, let's understand the theoretical concepts related to trigger. So as I told in first point, we can perform custom operations before or after events to record. So in that way, we can have two types of triggers before triggers and after triggers. So it is used, I'm talking about before triggers, so it is used to update or validate record values before saved to database. So before record is saved into database, if you want to do some updation in the record and you want to validate that record, so you can do that. And after triggers, so it is used to access field values that are set by the system such as IDs and to make changes in other records. The records that fire the after trigger are read only. So if you are using after events, so in that case, record will be saved in the database and the ID will be available. And also you can update other records as well. So that also we will understand with the help of demo. So basically, I'm going to create a playlist for a big trigger in Salesforce. So this is the first video. So you need to watch more videos like how we can implement various kind of triggers. I will be creating those videos with the help of demonstrations. So this is the basic difference between before trigger and after trigger. Now, this is the syntax for creating trigger. So first you need to write the trigger, the name of trigger then on the name of the object on which that trigger will be fired. And here we need to write the trigger events and then block of code. Right. So these are the trigger events. For before we have three before insert before update and before delete. And for after we have after insert after update after delete and after and delete. So now this is a simple example that I'm going to implement in the org. So trigger name of trigger will be a count trigger on account object and it will run on before insert and for example, I just put the system. The system dot debug. So this message will be available in the debug locks and this trigger will fire whenever we create an account record. Right. So I'm going to implement this and also remember one thing. Try to create only one trigger on one object. Don't create many triggers on the objects. So try to create one trigger only for a particular object. So now move into the org. So from here you can click on new and here you can see a pics class and then a pics trigger option is available. So click on this here. You need to mention the name. So naming it as a count trigger. Object list is available so you can choose object then click on submit. So here you can see before insert is by default available and let's say if you want to run it for before update as well. So you can write like this. So you can use multiple events as well. So right now we are just inserting so I'm using before insert and then system dot debug. The message you can write anything. So here trigger before insert event is mentioned. So I'm writing that. So now I'm saving this and then you can see we need to create a count record. So this we can do from UI as well as we can do that from anonymous window. So here I'm creating count a cc equals to equals to trigger that insert a cc. Right. So I'm going to execute both the statements and I'm checking this checkbox so debug log will be opened and we can see the message. Okay. So insert fail because this validation rule is available. So active field should be populated. So in my org I have one validation rule. So let me show you that so that you can understand like we need to populate the custom. We need to values for the custom validation. So here you can see check active. This is available. So if it is blank active though then this rule will be fired. So as of now I'm just deactivating this or maybe instead of deactivating you can pass value of active field here as well. So now I'm executing this so you will see no error is available and it will open debug log automatically and there you will see this message available. So it is not opening the debug log. So I'm just clicking on the logs and here you can see the debug logs and clicking on debugs only. So here you can see trigger before insert event. So this output is available. So this is a simple simplest example, which is demonstrating like how we can use our trigger how we can create triggers in Salesforce. Right. So later on we will implement some difficult example as well. So now, next is trigger construct context variables. So till now we discussed briefly about the trigger events like before and after. Now we have some trigger context variable as well that we can use while implementing triggers. So all triggers define implicit variables that allow developer to access runtime context. These variables are contained in the system dot trigger class. Right. So first one is is executing so you need to use them like trigger dot is executing trigger dot is insert trigger dot update. So this way you need to call them. So if you are using is executing so it means it returns true if the current context of apex code is a trigger, not a VF base or web service or an execute anonymous API call. The next is is insert. So returns true if trigger was fired due to an insert operation from the Salesforce or sorry is incorrectly mentioned here. So basically it is Salesforce UI apex or API. So whenever you are creating any record or let's inserting any record. So you can check trigger dot is insert. So if record is inserted so it will be true and you can do whatever you want to perform in that block. Then is updates so returns true if trigger was fired due to an update operation from the Salesforce UI apex or API. Then next is is delete so returns true if trigger was fired due to a delete operation from the Salesforce UI apex or API. Then next is is before so returns true if the trigger was fired before any record was saved is after returns true if the trigger was fired after all records were saved. So this we need to mention in the first line of the trigger as in the previous example we mentioned so this trigger will run only on before insert event. So if you want it should run on after insert event as well. So after comma you need to mention that as well. Right. And then is undelete so returns true if the trigger was fired after the record is recovered from the recycle bin. And then size the total number of records and the trigger invocation both all and new. The next is new so this we use at trigger dot new. So it returns a list of new version of S object records right so whenever in the second point you can see this object list is available in insert update and undelete triggers and the records can only be modified in before trigger. Right. So whenever you are inserting upgrading or undeleting the records. So in that case this new will be having a list of new version of the S object records. Right. And that trigger dot new can be modified only in before trigger. Then next we have new map. So a map of IDs to the new version of the S object records. So this is basically a list and new map is basically having ID and the values as well in terms of records for the new version of the S object records and available in after insert before update after update and after undelete. Right. So new map won't be available in before insert. Right. Because in before insert that will run in before even so in before insert we won't be having ID of the record. So that will be having when after insert will be available. But in case of before update and after update we will be having ID because we are updating the existing record. Right. And after undelete. So at that time also we will be having the ID. So in all these cases you can use new map. Then we have old and old map. So old returns a list of old version of S object records. Right. So let's say you are at updating any record. So whenever you are updating the record. So it will be having two state. The existing values and the updated record which will be having the new values. So the existing will be available in the old and the new will be available in the new. So old list this trigger dot old context variable will be available in case of before update after update before delete and. Trigger so then we have old map. So a map of IDs to the old version of S objects records. So available in before update after update before delete and after delete triggers. Right. So this way these all are trigger context variable that we can use in trigger. So now moving forward. So this is the real example that is not having any system dot debug statement. So this will actually affect the record. Right. So now I'm going to implement this. So we already implemented a trigger. So I'm going to modify that. So this trigger will run in before insert event. And here you can see we are using a context variable that is trigger dot new. So whenever we are we are going to create a new record. So that record will be available into this new trigger dot new. Right. So let's say we are creating only one record. So that will be available in this trigger dot new. And if you are creating new records through bulk insertion through data loader or data import wizard. So those all new records will be available into this trigger dot new. So always implement your trigger with bulkification operation. So trigger dot new will be having a list. So one by one, each record will be having into this ACC variable and then you can apply the operations. So now here I'm going to modify this. So I'm applying for account ACC. Then we are using trigger dot new context variable. And here I'm writing ACC dot description. So this description is a field on account. And here we are storing test description. So what will happen whenever you are trying to insert or trying to create new account record. So in before insert this trigger dot new will be having that record and then this loop will be trading. And those records, those records will be modified with this description field and having the value as test dot description. So here you can see we are not performing any DML operation. So in case of before events, we don't need to fire any DML operation. But in case of after events, we can fire DML operations as well. So right now what will happen this trigger dot new will be modified automatically. Right. Because we are using before event context here. So I'm saving this. So record is saved now moving to UI. So from you I am this time I'm going to create the record. And here you can see this test trigger record is already available. Right. So if I open this, so let's check the description of this. So here you can see it is having nothing. Now I'm going to create one more account record. So I'm going to name it as test trigger one. Right. And I'm not putting anything in the description, but you will say a trigger will do that. So the part is created. And here you can see test description is automatically populated. So this is the work off before trigger. And yeah, so I hope this way you understood basics of a trigger like how we can use triggers in Salesforce. So right now I demoed you this thing on before insert only so we updated a particular field while inserting a record on account object. Now moving further. So here you can see we can also call apex method through trigger. So now, if we follow the best practice, so best practice says, don't implement the code in the trigger itself. Right. So to implement the code, you need to create a separate apex class that will be known as trigger handler. And in that apex class, you, you have to implement all the logic and the method those you will be implementing in that class that you need to call in the trigger like this. So here you can see trigger account trigger on account then before insert. And here we are calling account trigger handler class before insert method and we are passing trigger dot new as a parameter. Then this trigger dot new will be received in this new list because this is the before insert method that is available in this class. And here we are calling this method directly so here static is not mentioned, but while implementing this I will be using static, right. Because through static we can call the method directly with the name of class and rest of the for loop is same that we already implemented in the trigger. Right. So now I'm going to create a class whose name will be account trigger handler. So yeah, this class is implemented here now public static white. So name of method can be anything as per the requirement. So for demo purpose I'm naming it as before insert. And here I'm writing list of account new list. And from there I'm going to copy this and pasting it here. And instead of trigger dot new now I'm going to use new list here. Right. So this way this is trigger handler class that I implemented now what I need to do. I can use that here or I can call that here. So I'm copying the name of class. Then I'm copying name of method. So dot name of method and then you can pass trigger dot new. So this way you can do. And if you want to use those trigger context variables. So you can like, you can write like this if trigger dot is insert. And you can write it here. So let's say here you have before update as well. So in that case you can use these context variables. If you want like this method before insert should be called whenever a chord is created or inserted. So you can write trigger dot is insert. So in insert case only this method will be called. And later on you can write L safe trigger dot is update. And here you can write the code or you can call update method. So this way you can use these context variable. And let's say if you're using after insert as well, right, one event after insert. So, so here you can apply one more if like, or you can let's say you can write and trigger dot is before. So this way you can apply the condition. So if trigger dot is insert and trigger dot is before. So insertion is happening and before. So in case of before event this method will be called and for update this, whatever you write that will be running. And for insert you can create one more block and you can apply nesting as well like for trigger dot is insert then you can apply this way you can apply if. So what we are doing here I'm applying one more if so here I can write trigger dot is after right so this complete block. This block is for insertion and in case of insertion if it is is before then this method will be called and if it is is after then this method whatever you write will be called. And if you are not inserting any record you are updating the record so else part will be executed and accordingly you can write more context variables. So I'm saving this right so here you can see everything is available so trigger handler classes available we are calling its method into trigger so this way what will be the benefit of this. When you will be opening the trigger so you you just need to identify like for which event or for which context, which trigger handler class and which method we are calling. And later on in this method in this class you can implement separate methods so this way you can easily manage your trigger and its handler class. Right, so again if I try to create one more record. So I'm naming it as test trigger to clicking on save. If I see the details so here you can see test description is available here also. So this way. I hope you understood brief idea about how to implement trigger and how we can use events and context variable and how we can implement the complete code in trigger itself and how we can divide the. Calling and definition like we are calling the methods in trigger, including events and context variable and we are defining the actual process in the trigger handler class. So moving forward so yeah this is all about basics of trigger in this video so. Other videos you can find in the playlist on YouTube, as well as you can go to this website study Salesforce.com so they're also you will find more videos related to Salesforce and apex triggers as well. So I hope you understood whatever I demonstrated in this video. So don't forget to watch more videos related to apex trigger so that you can understand the difference between before insert before update or after insert after update before delete after delete or after and delete so I will be covering all the scenarios in different different videos so that you can understand triggers completely. Thank you for watching this video.