function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Brig Larimer 10Brig Larimer 10 

How can I detect/disable active assignment rules in Apex?

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.
Anupam Bansal (Capgemini)Anupam Bansal (Capgemini)
Hi Brig,

It looks like an interesting problem to me. In the Documentation for Database.DmlOptions here  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_database_dmloptions.htm , it is mentioned that Database.Dmloptions are used for any records which are inserted / updated using DML operations and not via User interface. So even if you are writing the code snippet mentioned in your question, it will not be used. So, if you want to do all the things that you have mentioned, you will probably need to write a VF Page wherein you can grab different fields on the user interface and also allow a custom checkbox (VF Page only) the same as "Assign using Active Assignment Rules" and then do whatever you want to do based on what user inputs on the VF page. 

Thanks,
Anupam