+ Start a Discussion
HamptonHampton 

Help with Recurssion Error

Hello:

 

I am getting a recurrsion error on the trigger below and am not sure how to correct it? I am trying to update the field Orders__c on custom object Resweep_Assignment__c with a count of child records Assigned_Address__c where Service_Order_Number__c != ''

 

Any help?

 

Thanks,

 

Hampton

trigger OrderCount on Assigned_Address__c (after delete, after update) {
 
Set<ID> ResweepIds = new Set<ID>();
  if(trigger.isInsert || trigger.isUpdate){
    for(Assigned_Address__c assigned : trigger.new){
         if(assigned.Rep__c != '')
      ResweepIds.add(assigned.Resweep_Assignment__c);
    }
  }
 
 if(trigger.isDelete){
    for(Assigned_Address__c assigned : trigger.old){
      ResweepIds.add(assigned.Resweep_Assignment__c);
    }
  }
 
  Map<ID, Double> ResweepMap = new Map<ID, Double> ();
  for(AggregateResult ar : [select Resweep_Assignment__c, count(ID)
    from Assigned_Address__c where Resweep_Assignment__c in : ResweepIds and Service_Order_Number__c != '' and Rep__c != '' group by Resweep_Assignment__c]){
      ResweepMap.put((ID)ar.get('Resweep_Assignment__c'),(Double)ar.get('expr0'));
  }
 
  List <Resweep_Assignment__c> ResweepToUpdate = new List<Resweep_Assignment__c>();
  for(Resweep_Assignment__c a : [Select Id, Orders__c from Resweep_Assignment__c where Id IN :ResweepIds]){
    Double OrderCount = ResweepMap.get(a.Id);
    a.Orders__c = OrderCount;
    ResweepToUpdate.add(a);
  }
 
  update ResweepToUpdate;
}

 

Avidev9Avidev9

Not sure why you are getting the error.

Try moving the logic into a class Something like.

 

public class TriggerHelper {
    private static isExecuted = false;
    public static void process(List < Assigned_Address__c > triggerNew, List < Assigned_Address__c > triggerOld) {
        if (!isExecuted) {
isExecuted = true; Set < ID > ResweepIds = new Set < ID > (); if (trigger.isInsert || trigger.isUpdate) { for (Assigned_Address__c assigned: triggernew) { if (assigned.Rep__c != '') ResweepIds.add(assigned.Resweep_Assignment__c); } } if (trigger.isDelete) { for (Assigned_Address__c assigned: triggerold) { ResweepIds.add(assigned.Resweep_Assignment__c); } } Map < ID, Double > ResweepMap = new Map < ID, Double > (); for (AggregateResult ar: [select Resweep_Assignment__c, count(ID) from Assigned_Address__c where Resweep_Assignment__c in : ResweepIds and Service_Order_Number__c != '' and Rep__c != '' group by Resweep_Assignment__c ]) { ResweepMap.put((ID) ar.get('Resweep_Assignment__c'), (Double) ar.get('expr0')); } List < Resweep_Assignment__c > ResweepToUpdate = new List < Resweep_Assignment__c > (); for (Resweep_Assignment__c a: [Select Id, Orders__c from Resweep_Assignment__c where Id IN: ResweepIds]) { Double OrderCount = ResweepMap.get(a.Id); a.Orders__c = OrderCount; ResweepToUpdate.add(a); } update ResweepToUpdate; } } }

 Call above method from trigger by passing trigger.new and trigger.old

 

 

Bhawani SharmaBhawani Sharma
You need to recheck your triggers conditions and execute the triggers only if required. Like if OldVal != newVal etc.

Also use some static boolean flag to handle yhe execution. Execute the trigger only when this flag is check and mark it uncheck when you do not want to execute the trigger.