0

I have a fairly simple Process Builder where, if leads meet certain conditions, we will re-run the assignment rules. This is done with a scheduled action 0 Hours After LastModifiedDate that calls an invocable method to set the DML Options and update the leads.

This works just fine if only one record is updated to meet the conditions, but fails for multiple records at the same time. I can see that the scheduled actions get created in the Paused and Waiting Interviews list, but nothing actually happens. I don't receive any error emails, but the leads do not get reassigned.

I've looked around SFSE and have seen a couple of similar questions, but nothing that has an answer that addresses my issue. The records still meet the criteria, so they shouldn't be getting cancelled.

Process: Process Builder Conditionenter image description here

Apex:

public class RunLeadAssignmentRules {
    @InvocableMethod
    public static void assignLeads(List<Id> leadIds)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            Lead leadsToAssign=[select id from lead where lead.id in :leadIds];
            leadsToAssign.setOptions(dmo);
            update leadsToAssign;
   }
}

What is going on here that I'm missing?

1

As it turns out, I'm just a little slow today and didn't realize that my method was improperly written.

I needed to change to this:

public class RunLeadAssignmentRules {
    @InvocableMethod
    public static void assignLeads(List<Id> leadIds)
    {
            Database.DMLOptions dmo = new Database.DMLOptions();
            dmo.assignmentRuleHeader.useDefaultRule= true;          
            List<Lead> leadsToAssign=[select id from lead where lead.id in :leadIds];
            Database.update(leadsToAssign,dmo);
   }
}

I was able to discover this by refactoring to use an immediate action in PB to call an @invocable method which just passed off to an @future method for processing. I was then able to see the error in the @future job of List has more than 1 row for assignment to SObject.

D'oh!

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.