0

We are having an issue where our lead assignment rules are not being processed. I have verified that the rule is active.

We have data integration services in our org that update our leads via data integration rules. These updates do not trigger the lead assignment rules. The goal is to force them to always run as we have a formula field that is used to determine when the lead assignment rule should and should not run. The formula field is checked as the first step in the assignment rule.

I started by following this article. I have an invocable method:

@InvocableMethod(label='forceAssignmentRules' description='forces assignment rules to run')
public static void forceAssignmentRules(List<Id> leadsIds) {...}

This method accepts a formula value in the PB of lead.Id.

I have also tried:

@InvocableMethod(label='forceAssignmentRules' description='forces assignment rules to run')
public static void forceAssignmentRules(List<Lead> leads) {...}

Since PBs are called in the after part of the execution order I am making sure to query for my leads:

List<Lead> leads = [SELECT Id FROM Lead WHERE Id IN :leadsIds];
--- OR ----
 leads = [SELECT Id FROM Lead WHERE Id IN :leads];

I am defining my DML options as:

Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.UseDefaultRule = true;
---- OR ----
Database.DMLOptions dmo = new Database.DMLOptions();
Database.DMLOptions.AssignmentRuleHeader asr = new Database.DMLOptions.AssignmentRuleHeader();
asr.UseDefaultRule = true;
dmo.assignmentRuleHeader = asr;

And assigning this dmo to my records by:

database.update(leads, dmo);
---- OR ----
for (Lead l : leads) {
    l.setOptions(dmo);
}
update leads;

I have added debug lines into my code so I know that my code is running and there are no errors occurring during execution. With a test lead, if I click edit and go to the bottom and select the option to use assignment rules, the owner is changed as expected. Change the owner back and using the same lead with the above code, the owner does not change.

Future methods are not an option as one of the data services processes via scheduled batchables and future methods cannot be called form batchables.

I have tried a queueable but that created an infinite loop. The queueable updated the leads and then re-enqueued itself. I am currently looking at possibly fixing this infinite loop but it would be nice to not have to use queueables. I suppose even if I did get the above issue fixed, an infinite loop is still very possible.

I am unsure of what I am doing wrong, any help would be appreciated.

  • so in the data integration scenario, your debug shows that the invocable apex is running ? – cropredy Nov 7 '19 at 22:45
  • That is correct – gNerb Nov 7 '19 at 22:46
  • and the debug does not show that Lead Assignment rules execute (these would be under (Work)flows ? (as an aside, the queueable infinite loop suggests the lead is updated and then the PB re-executes the invocable apex etc.) – cropredy Nov 7 '19 at 22:48
  • There is this bit WF_RULE_EVAL_BEGIN Assignment but when looking at the formulas that are being evaluated within that WF they are not in the assignment rule, not sure where they are AND that appears before the code above executes so I'm mostly positive that there are no assignment rules running. – gNerb Nov 7 '19 at 23:05
  • The assignmentRuleHeader Property spec states: The assignmentRuleHeader property specifies the assignment rule to be used when creating a case or lead. *** It does not state any support for Lead/Case updates. I understand this was working previously (that post is from 2015) - my first assumption is that some Salesforce change made this stop working. I'm using annonymous apex with that invokable code and nothing - so even removing the process builder execution order and all of your complex context, the update is not triggering assignment rules to re-evaluate and assign the lead using DMLOptions – Glen De Marcos Nov 8 '19 at 0:15
0

EDITED 11/08/2019 9am pst

Note: Found sf bug while testing. On my assignment rule, I had to deselect the active flag, save, then select it again and save again. Once I did this and ran the update on the list of leads it worked and the UPDATE did fire the re-assignment (contrary to my prior assumption in my previous comment)

ANSWER

After testing your code in the same exact scenario I got the correct result and leads were reassigned. I used PB and invokable apex for POC. I noticed a change I had to do to fix your code and is that you CAN'T use the incoming List parameter as a dynamic binded param in the SOQL query directly because it throws a Bad rightOperand type: got SObjectScriptRow: operator e dataType 1 error. To solve this I generated a new List (called leadsFinal) by iterating over the incoming and adding to the new one. You stated that you were logging the code to verify no errors were occurring in apex, but your debug levels might be lower than needed to capture the automated process actions which throw this error. Hope this update helps you solve this issue:

enter image description here

My invokable:

public class testInvoke{
@InvocableMethod(label='forceAssignmentRules' description='forces assignment rules to run')
public static void forceAssignmentRules(List<Lead> leads) {
    // Begin new list instatiation and populate
    List<Lead> leadsFinal = new List<Lead>();
    for(Lead ll : leads)
        leadsFinal.add(ll);
    // End new list process
    leads = [SELECT Id FROM Lead WHERE Id IN :leadsFinal];
    Database.DMLOptions dmo = new Database.DMLOptions();
    dmo.assignmentRuleHeader.UseDefaultRule = true;
    for (Lead l : leads) {
        l.setOptions(dmo);
    }
    update leads;
}
}
|improve this answer|||||
  • Thanks, as stated in my question, when I check the box manually there is no error and everything works so I don't think this answer applies to me. I do appreciate your effort and it sounds like you were able to resolve your own issue which is great! – gNerb Nov 8 '19 at 15:02

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.