Avoid recursion of Trigger

A

Recursive trigger is frequently arising issue in Salesforce triggers. Recursion in trigger means a trigger calling itself again and again. Recursion in trigger occurs when we’ve added some DML statements in trigger and same DML condition is used for trigger firing.

A recursive trigger is one that is called over and over, if not controlled will result in this error…

maximum trigger depth exceeded

for example,

trigger update_record on login__c(before insert){

insert new login__c();

}

In above code , when one login __c object record is created by the user, the trigger executes and then inserts another record of the same type, which causes the trigger to execute again and again.

To avoid such type of situation we need to use static variables .single copy of static variable is shared by all the instances of the same class.

public class Count {

public static boolean insert_once = true;

}

trigger update_record on login__c(before insert){

if(Count .insert_once)

{

Count.Insert_once=false;

insert new login__c();

}

}

In the same transaction,static variable don’t retain its value between different trigger context.

This will call the trigger only once and hence avoid recursion.

Hope this helps you.

Thank you!

 

 

About the author

trupti.dhoka
By trupti.dhoka

Category