2

I've done a lot of reading about database.dmloptions, assignmentruleheader, etc. the past week or so, but am unable to find a simple answer to a few questions that continue to linger:

  1. I have some apex logic built to facilitate lead assignment. I would like for this logic to respect if the user checks "assign using active assignment rules", and to possibly not fire (i.e. return;) when this is the case. How can I check whether this option was enabled by the user on lead insert/update in apex?

  2. After getting an affirmative result from #1, I would like to evaluate whether the lead would actually be assigned by the assignment rule after evaluation. This is an important step as I will still want my logic to fire if the assignment rules all evaluate to be false. How can I perform this check? I suppose I could insert a test lead using the assignment rule, query the result, then delete the test lead, but this seems perhaps more DML-intensive than it needs to be...

  3. Additionally, if I did decide in certain cases that after detecting this option, I wanted to undo/disable this (despite the user's preference), how could this be done? I have attempted to use this segment on a before trigger, which has no effect:

    for (lead l : trigger.new)
    {
        database.dmloptions dmo = new database.dmloptions();
        dmo.assignmentRuleHeader.useDefaultRule = false; // (attempt 1)
        dmo.assignmentRuleHeader.assignmentRuleId = null; // (attempt 2)
        l.setOptions(dmo);
    }
    

Any and all help appreciated!

Thanks much.

1 Answer 1

2

How can I check whether this option was enabled by the user on lead insert/update in apex?

There is no means of detecting this option.

How can I perform this check?

Not only can you not tell, but the API and Apex allow you to specify an inactive rule as well... Or the owner Id might have been specified directly as well. There's simply not enough information to determine if the assignment was manual or automatic, or if a rule was selected at all.

Additionally, if I did decide in certain cases that after detecting this option, I wanted to undo/disable this (despite the user's preference), how could this be done?

It can't. The process lives outside the realm of Apex, and by the time your trigger sees the data, it's too late to determine if the rule applies or not anyways. Assigning an owner will simply result in the value getting clobbered by the assignment rule step, which is after the after trigger step.

2
  • Thanks sfdcfox, this is just what I needed to know. It's very unfortunate there aren't more methods in apex for this :( Jul 12, 2016 at 3:08
  • @BrentThomas I believe we may one day be able to do something about it, but that day isn't today. Perhaps you should post an idea.
    – sfdcfox
    Jul 12, 2016 at 3:45

You must log in to answer this question.

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