+ Start a Discussion
montezmontez 

Trigger Looping for Lead Assignment Rule

When a lead is updated and the owner is X and the Matched Status is Enriched, run the default lead assignment rule. My code is below, but when it runs I get the error below. It looks like it is looping the updating over and over.

Can you tell me where I am going wrong?

Code:

trigger AssignLeadAfterClean on Lead(after update) {
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    
    // Get the related leads in this trigger.        
    List<Lead> relatedLeads = [SELECT ID,OwnerID,ivm__MatchedStatus__c FROM Lead
                               WHERE Id IN :Trigger.New];
    
    List<Lead> leadsToUpdate = new List<Lead>();
    
    // Iterate over the related leads
    for(Lead lead_n1 :relatedLeads) {      
        // Run the Lead Assignement Rule when the owner is 005C0000006pm5r and clean process has completed
        if ((lead_n1.OwnerID == '005C0000006pm5r') && (lead_n1.ivm__MatchedStatus__c == 'Enriched')) {
            lead_n1.setOptions(dmo);
            leadsToUpdate.add(lead_n1);
        }
    }
    
    // Perform DML on a collection
    update leadsToUpdate;
}


Error:

Error:Apex trigger AssignLeadAfterClean caused an unexpected exception, contact your administrator: AssignLeadAfterClean: execution of AfterUpdate caused by: System.DmlException: Update failed. First exception on row 0 with id 00QM0000006fq8JMAQ; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, AssignLeadAfterClean: maximum trigger depth exceeded Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J] Lead trigger event AfterUpdate for [00QM0000006fq8J]: []: Trigger.AssignLeadAfterClean: line 21, column 1
Best Answer chosen by montez
Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Montez,

Try below code! you need to tighten your entry criteria:
 
trigger AssignLeadAfterClean on Lead(after update) {

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    
    // Get the related leads in this trigger.        
    List<Lead> relatedLeads = [SELECT ID,OwnerID,ivm__MatchedStatus__c FROM Lead
                               WHERE Id IN :Trigger.New];
    
    List<Lead> leadsToUpdate = new List<Lead>();
    
    // Iterate over the related leads
    for(Lead lead_n1 :relatedLeads) {      
        // Run the Lead Assignement Rule when the owner is 005C0000006pm5r and clean process has completed
        if ((lead_n1.OwnerID == '005C0000006pm5r') 
		&& (lead_n1.ivm__MatchedStatus__c == 'Enriched')
		&& (lead_n1.ivm__MatchedStatus__c != Trigger.oldMap.get(lead_n1.Id).ivm__MatchedStatus__c)) {
            lead_n1.setOptions(dmo);
            leadsToUpdate.add(lead_n1);
        }
    }
    
    // Perform DML on a collection
    if(!leadsToUpdate.isEmpty()) update leadsToUpdate;
}

This trigger should execute only once when Lead.ivm__MatchedStatus__c field values changes to "Enriched" from something else.

Hope this helps! If yes, mark it as solution.

All Answers

Sumitkumar_ShingaviSumitkumar_Shingavi
Hello Montez,

Try below code! you need to tighten your entry criteria:
 
trigger AssignLeadAfterClean on Lead(after update) {

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    
    // Get the related leads in this trigger.        
    List<Lead> relatedLeads = [SELECT ID,OwnerID,ivm__MatchedStatus__c FROM Lead
                               WHERE Id IN :Trigger.New];
    
    List<Lead> leadsToUpdate = new List<Lead>();
    
    // Iterate over the related leads
    for(Lead lead_n1 :relatedLeads) {      
        // Run the Lead Assignement Rule when the owner is 005C0000006pm5r and clean process has completed
        if ((lead_n1.OwnerID == '005C0000006pm5r') 
		&& (lead_n1.ivm__MatchedStatus__c == 'Enriched')
		&& (lead_n1.ivm__MatchedStatus__c != Trigger.oldMap.get(lead_n1.Id).ivm__MatchedStatus__c)) {
            lead_n1.setOptions(dmo);
            leadsToUpdate.add(lead_n1);
        }
    }
    
    // Perform DML on a collection
    if(!leadsToUpdate.isEmpty()) update leadsToUpdate;
}

This trigger should execute only once when Lead.ivm__MatchedStatus__c field values changes to "Enriched" from something else.

Hope this helps! If yes, mark it as solution.
This was selected as the best answer
Sumitkumar_ShingaviSumitkumar_Shingavi
You new changes looks fine. It will still work as expected. Happy that it helped!
montezmontez

Thank you @Sumitkumar_Shingavi, that works perfectly.
I added an OR option for the IVM_MatchedStatus__c.
It is still working, but if you see any issues, please point them out.

Thank you again.

 
trigger AssignLeadAfterClean on Lead(after update) {

    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.useDefaultRule= true;
    
    // Get the related leads in this trigger.        
    List<Lead> relatedLeads = [SELECT ID,OwnerID,ivm__MatchedStatus__c FROM Lead
                               WHERE Id IN :Trigger.New];
    
    List<Lead> leadsToUpdate = new List<Lead>();
    
    // Iterate over the related leads
    for(Lead lead_n1 :relatedLeads) {      
        // Run the Lead Assignement Rule when the owner is 005C0000006pm5r and clean process has completed
        if ((lead_n1.OwnerID == '005C0000006pm5r') 
        && ((lead_n1.ivm__MatchedStatus__c == 'Enriched') || (lead_n1.ivm__MatchedStatus__c == 'Not Enriched'))
        && (lead_n1.ivm__MatchedStatus__c != Trigger.oldMap.get(lead_n1.Id).ivm__MatchedStatus__c)) {
            lead_n1.setOptions(dmo);
            leadsToUpdate.add(lead_n1);
        }
    }
    
    // Perform DML on a collection
    if(!leadsToUpdate.isEmpty()) update leadsToUpdate;
}