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
NevDevNevDev 

RecursionHandler

Hi guys,

I was told that I need to create a RecursionHandler so that a trigger will autopopulate a checkbox when it creates a new record. It was suggested to me to use the below code. Do I just add this ontop of the trigger or where do I add it? 


public class RecursionHandler
{
    public static Boolean isrun = true;
}
 
Best Answer chosen by NevDev
Pankaj_GanwaniPankaj_Ganwani
Please double check your code of trigger:
 
trigger Object_Update on section_2_Balance_Sheet__c(after insert, after Update){
if(RecursionHandler.isrun)
{
List<section_2_Balance_Sheet__c> dupRecordList = new List<section_2_Balance_Sheet__c>();
List<section_2_Balance_Sheet__c> processedList = new List<section_2_Balance_Sheet__c>();
Set<Id> setConId = new Set<Id>();
for(section_2_Balance_Sheet__c ob : trigger.new){
     if(ob.Joint__c && ob.Client_balance_sheet__c!=NULL){
        setConId.add(ob.Client_balance_sheet__c);
		processedList.add(ob);
     }
}
map<Id,Contact> mapIdToContact = new Map<Id,Contact>([select Id, spouse__c from Contact where Id In : setConId and spouse__c!=NULL]);

for(section_2_Balance_Sheet__c ob : processedList)
{
	if(mapIdToContact.containskey(ob.Client_balance_sheet__c))
{
		section_2_Balance_Sheet__c objCloned = ob.Clone(false, true, false, false);
		objCloned.put('Client_balance_sheet__c',mapIdToContact.get(ob.Client_balance_sheet__c).spouse__c);
objCloned.put('Joint__c',true);
		dupRecordList.add(objCloned);
}
}
insert dupRecordList;
RecursionHandler.isrun = false;
}
}

 

All Answers

Pankaj_GanwaniPankaj_Ganwani
Hey Nevin,

Just create a separate Apex class for this by navigating following:

setup-->Apex Class-->New-->paste the above code.
NevDevNevDev
I did that Pankaj but the Joint_c checkbox remains blank. 
Pankaj_GanwaniPankaj_Ganwani
Please double check your code of trigger:
 
trigger Object_Update on section_2_Balance_Sheet__c(after insert, after Update){
if(RecursionHandler.isrun)
{
List<section_2_Balance_Sheet__c> dupRecordList = new List<section_2_Balance_Sheet__c>();
List<section_2_Balance_Sheet__c> processedList = new List<section_2_Balance_Sheet__c>();
Set<Id> setConId = new Set<Id>();
for(section_2_Balance_Sheet__c ob : trigger.new){
     if(ob.Joint__c && ob.Client_balance_sheet__c!=NULL){
        setConId.add(ob.Client_balance_sheet__c);
		processedList.add(ob);
     }
}
map<Id,Contact> mapIdToContact = new Map<Id,Contact>([select Id, spouse__c from Contact where Id In : setConId and spouse__c!=NULL]);

for(section_2_Balance_Sheet__c ob : processedList)
{
	if(mapIdToContact.containskey(ob.Client_balance_sheet__c))
{
		section_2_Balance_Sheet__c objCloned = ob.Clone(false, true, false, false);
		objCloned.put('Client_balance_sheet__c',mapIdToContact.get(ob.Client_balance_sheet__c).spouse__c);
objCloned.put('Joint__c',true);
		dupRecordList.add(objCloned);
}
}
insert dupRecordList;
RecursionHandler.isrun = false;
}
}

 
This was selected as the best answer
NevDevNevDev
Weird, it's working now but it didn't earlier today.

Okay so all I need to do now is to create the new Apex Class and paste the above code into it?
Pankaj_GanwaniPankaj_Ganwani
Yes, it is. If this is working fine for you, you can close this thread by marking it as solved.