Use default Lead Assignment rules

  • 2
  • Idea
  • Updated 4 years ago
  • Implemented
It's not possible to have a Lead form submitted by SKUID via sites to automatically run assigment rules. To solve this I created a trigger + class that runs assignment rules if a custom checkbox (hidden from users) are set to true. Maybe this quick fix can bee of use to someone else. See the code below:

Trigger
------------
trigger ReassignLeads on Lead (after insert, after update) {
List lIds=new List();
For (lead l:trigger.new){
if (l.IsConverted==False && l.Run_Assignment_Rules__c==true){
lIds.add(l.Id);
}
}
if (AssignLeads.assignAlreadyCalled()==FALSE){
system.debug('Assign already called? '+AssignLeads.assignAlreadyCalled());
AssignLeads.Assign(lIds);
}
}

-----

Class
-------
public with sharing class AssignLeads {
static testMethod void AssignLeadTest() {
//Basic code coverage test code - improve with bulk + asserts
List TestlIds=new List();
Lead newLead = new Lead(company='Testco', LastName='TestLast', FirstName='TestFirst', Run_Assignment_Rules__c=true);
test.startTest();
insert(newlead);
test.stopTest();

TestlIds.add(newLead.Id);
if (AssignLeads.assignAlreadyCalled()==FALSE){
AssignLeads.Assign(TestlIds);
}
}

public static Boolean assignAlreadyCalled=FALSE;
public static boolean assignAlreadyCalled(){
return assignAlreadyCalled;
}

@future
public static void assign(List lIds){
assignAlreadyCalled=TRUE;
List leads=[SELECT Id FROM Lead WHERE Id IN: lIds];
For (lead l:leads){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
l.setOptions(dmo);
l.Run_Assignment_Rules__c = false; // reset the checkbox

}
update(leads);
}

}
Photo of Peter Baeza

Peter Baeza

  • 2,868 Points 2k badge 2x thumb

Posted 6 years ago

  • 2
Photo of Anna Wiersema

Anna Wiersema

  • 10,890 Points 10k badge 2x thumb
Thanks for sharing, Peter! This is awesome.
Photo of Zach McElrath

Zach McElrath, Employee

  • 49,056 Points 20k badge 2x thumb
This has been implemented as of Skuid 4.15.8 - it is now possible to specify, at a row level, whether or not to run default Lead/Case Assignment Rules, as well as (for Leads only) to specify a particular Assignment Rule to run (if you have multiple defined in your org).

Support for various other DMLOptions has been added as well. 

See the release notes for Skuid 4.15.8 for more details (available from the Skuid releases page).