function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
eswarsfeswarsf 

Recursive Trigger Error

Hi ,

I have created the following trigger and classes.

Trigger----

 

trigger RecursiveTrigger on Employee__c (Before Update)

{
    if(!RecursiveTriggerClass.RecursiveFlag)   

{     

  Employee__c [] Emp = Trigger.New;       

RecursiveTriggerClass.recursiveTrigger();   

}

}

 

Class

 

public class RecursiveTriggerClass

{   

Public static Boolean RecursiveFlag = false; 

public static void recursiveTrigger()   

{       

Integer i=0;       

List <Employee__c> EUpdateList = new List<Employee__c>();       

Employee__c [] EList = [Select Id,Name,First_Name__c,Last_Name__C from Employee__c  where IdIN:Trigger.NewMap.KeySet()];               

for(Employee__C E : EList)       

{           

E.first_name__c = E.Last_Name__c+E.first_name__c;           

EUpdateList.add(E);           

i++;       

}       

System.Debug(i);       

RecursiveFlag = true;        

Update(EUpdateList);   

}

}

 

It was throwing "SELF_REFERENCE_FROM_TRIGGER" error though i have used static variable to prevent recursive firing.

I surprised when i have used "After Update", it was not throwing this error.

Kindly help me in this error.

 

Thanks,

Eswar.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

The error isn't related to recursive calling - I'd expect that part of your code to be working.

 

The error is because you are carrying out a DML update on a record which is part of the before trigger - that is not allowed, regardless of recursive calls etc.

All Answers

bob_buzzardbob_buzzard

This error means that you are attempting to perform an update DML call on an object that is already part of the trigger context (i.e. something that is part of trigger.old/new).  This is not allowed in a before trigger.

 

As you are in a before trigger, all the records are writeable, so rather than executing an update, you can simply set the name field on the records.  That way you don't need to worry about recursive calls etc.

 

E.g.

 

 

trigger RecursiveTrigger on Employee__c (Before Update)
{
   for(Employee__C E : trigger.new)       
   {           
      E.first_name__c = E.Last_Name__c+E.first_name__c;  
   }
}

These changes will then be persisted when the data is written to the database.

 

One thing though - every time the employee is updated this concatenation will occur, which I suspect isn't quite what you are looking for.

eswarsfeswarsf

Hi Bob,

Thanks for your reply.My primary intention in the code is to call the class and wanted to prevent recursive calling.

How can we prevent recursive calling in 'Before Update ' event Trigger using an static variable?

 

Thanks and Regards,

Eswar.

bob_buzzardbob_buzzard

The error isn't related to recursive calling - I'd expect that part of your code to be working.

 

The error is because you are carrying out a DML update on a record which is part of the before trigger - that is not allowed, regardless of recursive calls etc.

This was selected as the best answer
kritinkritin

Hi ...You can prevent from recursive calling only if you check Old and New values.

While passing values to the Class method you need to check if Old Values and New values are not same then pass to Class's method otherwise don't pass it to method.

 

Please update this meesage as Solution if it is acceptable.

eswarsfeswarsf

Thanks Kritin,for replying.

 

Thanks and Regards,

Eswar.