Saturday, May 18, 2019

Salesforce Interview Questions on trigger

1) What is trigger in Salesforce and what are trigger events?

We basically use triggers to perform an operation such as validation(Preventing wrong data entry),updation(updating related records).


Following are the events on which trigger fires,


Before insert
Before update
Before delete
After insert
After update
After delete
After undelete

2) What are trigger context variables in Salesforce? 


Following are the Trigger context variable available,

trigger.isbefore()
=>Check if the trigger is in before mode, If the trigger is in before mode it will return true.

trigger.isafter()
=>Check if the trigger is in after mode, If the trigger is in after mode it will return true.


trigger.isupdate()
Check if the trigger is in update mode, If the trigger is in update mode it will return true.

trigger.isdelete()
Check if the trigger is in delete mode, If the trigger is in delete mode it will return true.


trigger.isinsert()
Check if the trigger is in insert mode, If the trigger is in insert mode it will return true.


trigger.isundelete()
Check if the trigger is in undelete mode, If the trigger is in undelete mode it will return true.


trigger.isexecuting()
Check if the apex class method is getting called from apex trigger, If getting called return true.


trigger.new()
Stores new records.

trigger.newmap()
Stores new records with id's

trigger.old()
Stores old records.

trigger.oldmap()
Stores old records with id's.


3) What is the difference between Trigger.new and Trigger.old and Trigger.newmap and Trigger.oldmap ?

Trigger.new returns new records and Trigger.old return data before updates were done.

Trigger.newmap returns new records with id's and Trigger.oldmap return data before updates were done with id's.

4) Is the id of record changes if we undelete a deleted record?

No, It has the same id.

5) What is the use of trigger.isexecuting?

Suppose we have a method in apex class and we want this method to run only when the method is getting called from apex trigger than we can make use of trigger.isexecuting in apex class to check if the method is getting called from trigger .

6) How to restrict trigger to fire only once(Recursive trigger)? 


Recursion occurs when your trigger fires again and again.
The situation such as when you are using after update trigger and updating parent record and again on parent record, there is a trigger which updates child records. It can lead to an infinite loop.

To avoid this,


We can make use of static variable.

As an example

Inside trigger,

If(NewApexClass.booleanvar) //checking value to avoid second run
{
//calling Handler Class Method
HandlerClass.methodname();

}

Inside NewApexClass ,

public class NewApexClass{

public static boolean booleanvar=true;

}

Inside Handler Class,

public class HandlerClass{


public static  void methodname()
{
booleanvar=false;

}

}

7) How many trigger we should write on one object?

We should always follow the best practice and consider writing one trigger per object. If we have more than one trigger per object on same event say before insert than we cannot guarantee the order of execution.  

8) What is the difference between database.insert and insert?

Using insert if one record fails entire operation is stopped and none of the record is inserted into database, whereas with databse.insert partial insertion is supported.

9) Is it possible to call batch class from trigger.

Yes.

10) What are the the context variable available with before insert event?

Only Trigger.new is available.

11) What are the the context variable available with after insert event?

Trigger.new and Trigger.newMap.

12) What are the the context variable available with before update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

13) What are the the context variable available with after update event?

Trigger.new, Trigger.old, Trigger.newmap and Trigger.oldmap

14) What are the the context variable available with before delete event?

Trigger.old and Trigger.oldMap.

15) What are the the context variable available with after delete event?

Trigger.old and Trigger.oldMap.

16) What are the the context variable available with after undelete event?

Trigger.new and Trigger.newMap.

17) What is the condition to call a method from trigger which is making callout?

The callout should be asynchronous.

18) What will you do if a method inside apex class need to be executed only when it is getting called from trigger?

We will use trigger.isexecuting in apex class to check if the method inside apex class is getting called from trigger and will execute the method if getting called from trigger.

No comments:

Post a Comment