0

I've seen my share of recursion issues, but this one is strange.

I have fairly simple code triggering lead assignment rules to run when a field (New_Company_HQ_State__c) is changed on a lead. The code involves updating the record a second time, but instead of the trigger firing twice, it fires 4 times. The strangest part is that the debug logs show that New_Company_HQ_State__c is changed on the record twice, on the 1st and 3rd updates, not the 2nd or 4th(?!?), and yet the trigger fires 4 times, even though the code only updates the record if a change is made.

You can see the debug log below as well. What I'm wondering is - does triggering lead assignment cause an extra update to occur, in addition to the one that happens at Database.update(LeadsToUpdate, DMO)? That would seem awfully odd. But even then, I can't figure out why the trigger is behaving as if New_Company_HQ_State__c is changed twice, when in fact it is changed only once.

Many thanks

public static void RunLeadAssignmentRules(List<Lead> Leads, Map<Id, Lead> IdToOldLead){

        List<Lead> LeadsToUpdate = new List<Lead>();

            system.debug('>>>>>>>>RunLeadAssignmentRules');
        for(Lead L : Leads){
            Lead OldLead = IdToOldLead.get(L.Id);
                system.debug('>>>>>>>>OldLead.New_Company_HQ_State__c = '+OldLead.New_Company_HQ_State__c);
                system.debug('>>>>>>>>L.New_Company_HQ_State__c = '+L.New_Company_HQ_State__c);    
            if(L.Allbound_ID__c != null && 
              (L.Region__c != OldLead.Region__c ||
               L.New_Company_HQ_Country_Code__c != OldLead.New_Company_HQ_Country_Code__c ||
               L.New_Company_HQ_State__c != OldLead.New_Company_HQ_State__c || 
               L.Revenue_Class__c != OldLead.Revenue_Class__c ||
               L.Website != OldLead.Website)){ 
                  LeadsToUpdate.add(new Lead(Id = L.Id));

            }
        }   system.debug('>>>>>>>>LeadsToUpdate = '+LeadsToUpdate); 

        Database.DMLOptions DMO = new Database.DMLOptions();
            DMO.AssignmentRuleHeader.useDefaultRule = true;

        if(!LeadsToUpdate.isEmpty()){
            try{
                Database.update(LeadsToUpdate, DMO);        
                    if(Test.isRunningTest()) {integer intTest =1/0;}               
            }
            catch(Exception e){
                system.debug('>>>>>>>>Exception occurred trying to run assignment rules on updated Leads: '+e.getMessage());
                SystemLogUtil.ExceptionLog('LeadHandler.RunAssignmentRules', e , 'Exception occurred trying to run assignment rules on updated Leads');
            }                 
        }   

    }

enter image description here

1
  • if(Test.isRunningTest()) {integer intTest =1/0;} is not best practice - I know you are trying to do test coverage of the catch but an easier way is to just pass leads w/o a Company as that is a required field
    – cropredy
    May 5, 2020 at 23:53

2 Answers 2

0

Without seeing your logs or setup, I presume that there's probably at least one automatic update somewhere. The first trigger entry calls the second, during which the values don't change, but then back after the first entry, a process or workflow causes a recursive update, so the first part runs again, sees the original changes, executes again, with the recursive update again seeing no change, and then back out to finish up. Something like this:

  • BEFORE_UPDATE
    • START
    • END
  • AFTER_UPDATE
    • START
    • BEFORE_UPDATE
      • START
      • END
    • AFTER_UPDATE
      • START
      • END
    • END
  • WORKFLOW/PROCESS
    • BEFORE_UPDATE
      • START
      • END
    • AFTER_UPDATE
      • START
      • BEFORE_UPDATE
        • START
        • END
      • AFTER_UPDATE
        • START
        • END
      • END

As you can see, four execution calls from the trigger. There's only minimal recursion, and this should probably be harmless. That said, if you wanted to optimize, you should simply allow the user to set the assignment rule flag, or force it at a higher level (e.g. override the UI with custom code, etc).

0

Try to add a static flag and if you don't need trigger to be executed before and update try to you Trigger.isBefore or Trigger.isAfter

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .