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
gvgv 

Recursive Update issue

Hi All

 

I have a before update trigger on a object. I am getting a list of all records matching a particular id (trigger.old) and looping through the list .

 

The problem is while looping thorugh if a particualr check box is checked I want to uncheck it. So I do that add it to another list and do a update and run into the recursive update issue .

 

 I am stuck. This is the offending code in the class which is called by the before update trigger

 

 

public static void beforeUpdate(List<Contact__c> ContactList){ i Set<Id> accountIds = new Set<Id> (); for (Contact__c con: ContactList){ if (con.account__c!=null) accountIds.add(con. account__c); } List< Contact__c > updateContacts = new List< Contact__c >(); List< Contact__c > ExistingContactList = ([Select Id, e.Name,active__c from Contact__c e where e.account__c.Id in : accountIds order by name desc]); for(Contact__c contactcounter : ExistingContactList ){ if (contactcounter .active__c == true){ contactcounter .active__c = false; updateContacts.add(contactcounter ); } }//for loop //update updateContacts; }

 

 

 

 When I execute this I get the recursive error as I am callin the update updateocntacts inside a before update trigger. But I want to change the value of all contact records active value if its true to false . How to achieve that?

 

Thankss 


 

RyanhRyanh
I had the same problem. You have to create a helper class that will help you manage the recursion. See page 150 in the Force.com Cookbook: http://www.developerforce.com/events/forcedotcomcookbook/registration.php
jpwagnerjpwagner

I'm not clear on your task, but if this is before update on the object you are attempting to change the flag (Contact__c), can't you call the method you want to perform directly rather than off of a different trigger?

 

 

ie:

...

for(Contact__c contactcounter : ExistingContactList ){

  if (contactcounter.active__c == true){

    contactcounter.active__c = false; 

    updateContacts.add(contactcounter);

  }

}

  DoSomethingToRecordsNowDeActive(updateContacts);

...

gvgv

Thanks Ryanh  and JPWagner for responding.  Will try both options to find out which is easier for me to write

 

JPwagner , My task is to loop through a list of records and make all the previous checked records as unchecked and only the current one that's being updated as checked.

 

In your way,I assume you are referring to have a method which will update the flag and call that method inside the update trigger?

 

correct me if am wrong