 Hello, everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. In this video, I'm going to discuss about Apex Trigger related interview questions and answers. So these questions and answers will be beneficial for freshers as well as experienced candidates so that you can just revise the concepts. So first is like what is Trigger? So it is a common question that is asked. So triggers are initiated when a record is inserted, updated, deleted or undeleted. We can perform custom operations before or after events to records. Use triggers to do operations that cannot be done by point and click tools provided in Salesforce like flows. We can do things using triggers that we can do through Apex including execution of SOQL and DML or we can call custom methods as well. Triggers can be created for both standard and custom objects. By default triggers are active as you create them. So this is about trigger. Then next question can be what is the syntax of trigger? So you can use a keyword trigger then name of trigger on object API name and then trigger events. So generally, for example, if you are creating a trigger on account object, so trigger name we will write as account trigger. And after on you can write the API of the account. So that is account. If you are using a custom object or if I say like you are creating a trigger on custom object. So custom object API should contain underscore underscore C here. And then you need to put the events inside brackets so event I will be covering in the later questions. And inside braces you can write the code which you want to run or you can you can call the Apex methods as well. Now if you want to learn like how we can write trigger how different different use cases are implemented in trigger. So just visit study salesforce.com and there you will find separate videos for all the trigger concepts and scenarios. Now what are types of trigger? So we have two types of triggers. One is before triggers and one is after trigger. So use before trigger. It is used to update or validate record values before saved to database. So for example, if you suppose you are creating any account record and upon account record creation if you want to update that account record particular field only. So in that case you can use before triggers. Now if you are inserting any account record and you want to do something on the related record. So in that case you can use after trigger. So it is used to access field values that are set by the system such as IDs. So in case of after trigger IDs will be available and to make changes in the related and other records. So it is preferred. The records that fire the after trigger are read only. So in case of after trigger. So we have one context variable that is trigger dot new. So in case of after trigger that trigger dot new will be in read only form. But in case of before trigger it will be in editable. Now what are the trigger events? So we have before insert before update before delete after insert after update after delete and after under new. So these are the seven events that you can use and you don't need to use all of them together. So as per the requirement you can write these events in your trigger. Now this is important trigger context variables. So all trigger defines implicit variables that allow developer to access runtime context. These variables are contained in the system dot trigger class. So first we have is executing so it returns true if the current context of apex code is a trigger not a VF page of web service or an execute anonymous API call. Then we have is insert so it returns true if trigger was fired due to an insert operation from the Salesforce UI apex or API. Then is update is similar so it will be like returning through if a trigger was fired due to an update operation. Then is delete returns true if trigger was fired due to delete operation. So these is insert is update is before basically we write in the trigger code so that we can identify if you want to run the apex method. So in which way we need to call the apex method. So if you want to run any apex method whenever your record is inserted, so you need to check whether trigger is running and is insert context or not like that. Then is before is after so is before returns to the trigger was fired before any record was saved and after it will fire when record is saved to the database. Until it returns true if the trigger was fired after a record is recovered from the recycle bin. Size it checks total number of reports in a invocable invocation both old and new. Then we have another question that is trigger dot new versus trigger dot new map. So new basically returns a list of new version of S object records. So this S object list is available in case of insert update and undelete triggers and the records can only be modified in before trigger so basically you can write it like trigger dot new. So it is available in case of insert update and undelete only and if you have before trigger context. So in that case, you can just modify its value but if you are writing something in after context then you cannot modify it. Then we have new map. So a map of IDs to the new version of S object records. So it is available in insert before update after update after undelete triggers. So it is basically a map which contains ID and records. Then we have trigger dot old versus trigger dot old map. So old basically returns a list of old version of S object records available in before update after update before delete and after delete. Right. So in case of insert there won't be any data in old because in insert we have only new version. But in case of update we have two versions one is old which is having the old values of the record and one will be having the new values of the record so old state will be stored in this trigger dot old. Then next is old map. So a map of IDs to the old version of S object records available in before update after update before delete and after delete triggers. Next is can we call a apex class through trigger. So yes, and it is best practice if you create separate apex class for trigger. So it is suggested that don't write the business logic in the trigger itself instead create separate methods in your trigger handler class and then call those methods in the trigger. So here's the example. So you can see trigger then account trigger on account and event is before insert. Then we are calling this before insert method which is defined in account trigger handler class. And as a parameter we are passing trigger dot new context variable, which is later in the class received in a new list variable trigger best practices. So write one trigger per object. Bulkify your port so that you don't hit the governor limit. Logic less trigger so as in previous question I explained in trigger don't write the logic instead write the logic in your apex class and those methods you can call in the trigger. Avoid using SQL or DML inside for loop to avoid hitting governor limits so don't write SQL and DML inside the loop. Avoid nested loops and try to use map instead. So it will save your runtime and use static Boolean variable to avoid recursive trigger. What is recursive trigger so it is also important question if you are like giving interview as a fresher or as an experienced candidate as well. And if you want to learn like how these concepts work so all those reported videos are already available on study salesforce.com so do visit. In some scenarios it can happen that the result of the trigger can end up calling the same trigger again and again so this is the scenario of recursive trigger. This situation is known as recursive trigger to avoid this scenario we should create a static variable and check the value of that variable before we execute anything in the trigger. Now real time use case so when you update a report from UI then trigger will be called now in trigger as well if you apply update DML so it will call the same trigger again and again and ends up as recursive. Can we apply validation through trigger yes we can use add error method to apply validation through trigger so I created an interesting video on that which is available on study salesforce.com so you can do watch. Can a trigger call a batch class yes we can call a batch class and trigger as we do in the normal apex code and if you don't remember the batch class concepts so you can watch them on study salesforce.com and I have also created. Question and answers for asynchronous apex concepts as well so you can do watch. Can a trigger make a call apex call out method yes we can call a call out method in apex trigger but the only condition is that it has to be an asynchronous call out because the trigger flow cannot wait on the response received by the call out method. What is trigger bulkification so it is important and you should know this questions answer. Triggers should be able to handle single records as well as bulk reports it means if you insert update delete single record then also your trigger should work properly. If you do bulk insertion bulk updation bulk deletion maybe through data loader or import results so in that case also your trigger should work fine. Even insert update delete in bulk we can do through apex code as well so for example through anonymous window if you are inserting five records so it will call trigger so your trigger should be capable to handle those five reports insertion so that is basically known as handling bulkification through trigger. So you should write triggers that operate on collection of S objects trigger should perform efficient SOQL and DML operations. Is there any limit on numbers of records defined on an object so we can define as many triggers on an object but it is recommended to have one trigger per object as the order of execution of different trigger is not guaranteed and any trigger can start execution first. So instead writing different different triggers you can write one trigger and you can define the business logic in separate apex classes or same apex classes in different methods. And those methods you can call in the trigger so that you can manage your trigger smartly and in that case you can maintain the order of execution inside that trigger Excel. So you can say like this method will call first and this second this third so this way you can control the execution sequence of the business logic order of execution in trigger so this is another interesting question. So we have like almost 20 steps here. So when you save a record with an insert update or absurd statement Salesforce performs the following events in order. So it loads the original record from the database or initialize the record for an absurd statement. Loads the new record field values from the request and overrides the old values. Executes record trigger flows those are configured to run before the record is saved. Then execute all before triggers. So once most system validation steps again and run custom validation rules, the only system validation that Salesforce doesn't run a second time. When the request comes from the standard UI added base is the enforcement of layout specific rules, then executes all duplicate rules, then saves the request to the database but does not commit yet. Executes all after triggers execute assignment rules execute auto response rules. Execute workflow rules execute escalation rules execute processes flows launched by processes order is not guaranteed, then executes entitlement rules, then execute record trigger flows those are configured to run after the record is saved. This record contains a roll up summary field or is part of a cross object workflow performs calculations and update the roll up summary field in the parent record. Then parent record goes to save procedure that the parent record is updated and a grant parent record contains a roll up summary field or is part of cross object workflow performs calculations and updates the roll up grand parent record, grand parent record goes through safe precision, then executes criteria based sharing evaluation, then commits or DML operation to the database. After the changes are committed to the database executes post commit logic such as sending email and executing and queued as synchronous apex jobs, including cueable jobs and future methods. So these are the steps which are included in order of execution and triggers. So these are the some questions that you can prepare for interviews. So these all questions are related to apex trigger. If you want to watch other topic related questions. So those are also available on my channel along with on this website study salesforce.com. Thank you.