1

I'm trying to run lead assignment rules from an invocable method, but it doesn't seem to be actually updating the lead assignment according to the rules (or at all!)

We have a process by which users can create a new lead from a support case, using a quick action (where the use assignment rules checkbox isn't available). When the lead is inserted (and is inserted by a service cloud user), a process builder calls an invocable apex class which looks like this:

public class AssignLeadsUsingAssignmentRules
{
    @InvocableMethod(label='Assign Leads Using Assignment Rules' Description='Run this to re-run assignment rules on any leads needing to be re-checked.')
    public static void AssignLeadsUsingAssignmentRules(List<Id> LeadIds){
        system.debug('In InvocableMethod - sending to future method');
        leadAssignFuture(leadIds);

   }

    @future
    public static void leadAssignFuture(List<Id> LeadIds){

        system.debug('in future method - lead ids: ' + leadIds);

        database.DMLOptions dmo = new Database.DMLOptions();
        dmo.assignmentRuleHeader.assignmentRuleId= '01Q0O0000003R7Q';
        //dmo.assignmentRuleHeader.useDefaultRule = true;

        list<Lead> leadList = new list<lead>{};
        for(lead l : [select id from lead where lead.id in :LeadIds]){
            l.setOptions(dmo);
            leadList.add(new Lead(id = l.id));

        }
        system.debug('DMO: ' + dmo);

        database.update(leadList);
   }
}

As you can see, I have:

a) put the "meat" of the code in a future method to ensure that it doesn't hit order of execution issues (is this the right thing to do?)

b) tried using both the default lead assignment rule and explicitly naming one, but neither option works. I'm not seeing any errors in my debug logs, and the 'DMO' debug call above returns this:

DMO: DMLOptions:[AllowFieldTruncation=null, AssignmentRuleHeader=AssignmentRuleHeader:[AssignmentRuleId=01Q0O0000003R7Q, UseDefaultRule=null], DuplicateRuleHeader=DuplicateRuleHeader:[AllowSave=null, RunAsCurrentUser=null], EmailHeader=EmailHeader:[TriggerAutoResponseEmail=null, TriggerOtherEmail=null, TriggerUserEmail=null], LocaleOptions=null, LocalizeErrors=null, OptAllOrNone=null]

After this runs, the owner of the lead is still just the individual who triggered the process, not the queue as it should be in the assignment rule.

I'm not quite sure what else I can try - any suggestions?

  • 1
    database.update(leadList, dmo); You can also probably get rid of that loop, I've never had to set the options on a per record basis. – gNerb Nov 1 '18 at 16:26
  • 1
    @gNerb more to the point, I think the per-record version is actually broken, or perhaps only works in certain conditions. I've never seen it work, and the documentation doesn't help. – sfdcfox Nov 1 '18 at 23:36
  • Thanks guys - I took out the loop and just used @gNerb's suggestion of database.update(leadList, dmo) - worked a charm. I'll create an answer in case anyone else comes this way looking for the answer. – Charlotte G Nov 5 '18 at 10:05
2

With thanks to gNerb and sfdcfox, here's the altered code to fix the issue:

public class AssignLeadsUsingAssignmentRules{

    @InvocableMethod(label='Assign Leads Using Assignment Rules' Description='Run this to re-run assignment rules on any leads needing to be re-checked.')

       public static void AssignLeadsUsingAssignmentRules(List<Id> LeadIds){
       system.debug('In InvocableMethod - sending to future method');
       leadAssignFuture(leadIds);

    }

    @future
       public static void leadAssignFuture(List<Id> LeadIds){

       system.debug('in future method - lead ids: ' + leadIds);

       database.DMLOptions dmo = new Database.DMLOptions();
       dmo.assignmentRuleHeader.useDefaultRule = true;

       list<Lead> leadList = [select id from lead where lead.id in :LeadIds];
       system.debug('DMO: ' + dmo);

       database.update(leadList, dmo);
   }

}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.