You need to sign in to do that
Don't have an account?

How to route LEADs back through LEAD Assignment rules
We have LEAD routing working but have a case where LEADs will fall into a special queue for various reasons (say, product download is outside of the ownership of the current LEAD owner). We want to find a way to automatically route these LEADs in this queue back through the LEAD assignment rules.
Currenly, we manually have to go into each LEAD in this queue, edit and select assign using active rules (checkbox at bottom of LEAD record).
Any ideas?
The code has evolved some but it's working in the sandbox. Now, it's off to writing a "class". Thanks for the help John.
trigger <name> on Lead (after insert, after update) {
Map<Id,Group> QueueId= new Map<Id,Group>( [select id from group where name ='<queue name>' limit 1]);
List<Lead> leadstoUpdate = new List<Lead>();
For (lead l:trigger.new){
system.debug('*******Error: ');
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
if(!QueueId.containsKey(Trigger.OldMap.get(l.id).ownerId))
{
if (QueueId.containsKey(l.ownerId) )
{
l.setOptions(dmo);
system.debug('*******Error: ');
Lead l2 = new Lead (id=l.id);
l2.setOptions(dmo);
leadstoUpdate.add(l2);
}
}
}
if(!leadstoUpdate.isEmpty())
{
update leadstoUpdate;
}
}
All Answers
So you're saying Lead Assignment rules aren't assigning to the right person? Assuming you can't simply change your assignment rule entries, you could create a trigger to check Lead.Owner for the desired queue & the reassign automatically.
Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule= true; for (lead l: trigger.new){ if (l.ownerId='insertidhere') l.setOptions(dmo); } }
John,
They work perfectly on the initial time through the assignment. The issue we face is that the LEAD may need to change hands for various reasons and we can't expect the Sales rep to manually reassign their LEADs. So based on workflow we have a nightly job that will move these LEADs from the owner and place them in a queue where we will go and move them manually instead of the Sales Reps. We want to automate this and what you provide may work.
In either case what you provided would re-evaluate the LEAD owner against the Assignment rules and reassign if needed?
Yes - the above code will reassign any leads specified. To get the leads from your queue, you'd have to specify the ownerID =QueueID.
trigger reassignLeads(before update){ String QueueId='insertYourQueueIdHere'; For (lead l:trigger.new){ if (l.OwnerId=QueueId){ //insert the dmloptions code here } } }
trigger reassignLeads on Lead (after insert)
{
String QueueId='00G50000000ylW2';
For (lead l:trigger.new){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
if (l.ownerId='00G5000000ylW2')
l.setOptions(dmo);
}
}
This is what I have but on Line 7 I receive must be Boolean......
For "If" statements, you need:
==
trigger reassignLeads on Lead (after insert)
{
String QueueId='00G50000000ylW2';
For (lead l:trigger.new){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
if (l.ownerId=='00G5000000ylW2')
l.setOptions(dmo);
}
}
btw-I think you may need to include Database.Update(l);
I didn't have that in my example. Here's the fixed code:
trigger reassignLeads on Lead (after insert)
{
String QueueId='00G50000000ylW2';
For (lead l:trigger.new){
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule= true;
if (l.ownerId=='00G5000000ylW2')
l.setOptions(dmo);
Database.update(l);
}
}
Ah yes - I see the issue: DMLOptions needs a DML call to execute such as:"Database.Update(l);", but if you're in an update trigger, then you can't update the existing lead.
One option here is override the Save button with a VF page that calls an Apex method upon load. However, that doesn't help you for other mass updates such as from a List View, Import, or the Data Loader.
To cover all basis, you could have the trigger call an @future method to update the leads a split second after the update finishes:
public global class AssignLeads{
public static Boolean assignAlreadyCalled=FALSE;
public static boolean assignAlreadyCalled(){
return assignAlreadyCalled;
}
@future
public static void assign(List<Id> lIds){
assignAlreadyCalled=TRUE;
List<Lead> 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);
}
update(leads);
}
}
btw-this works in a "clean" org, but in one of my orgs (has my lead scoring app installed) it won't work due to another @future method "interfering" - it fails without an error despite the debug log showing the leads are updated, which is troublesome.
The code has evolved some but it's working in the sandbox. Now, it's off to writing a "class". Thanks for the help John.
trigger <name> on Lead (after insert, after update) {
Map<Id,Group> QueueId= new Map<Id,Group>( [select id from group where name ='<queue name>' limit 1]);
List<Lead> leadstoUpdate = new List<Lead>();
For (lead l:trigger.new){
system.debug('*******Error: ');
Database.DMLOptions dmo = new Database.DMLOptions();
dmo.assignmentRuleHeader.useDefaultRule = true;
if(!QueueId.containsKey(Trigger.OldMap.get(l.id).ownerId))
{
if (QueueId.containsKey(l.ownerId) )
{
l.setOptions(dmo);
system.debug('*******Error: ');
Lead l2 = new Lead (id=l.id);
l2.setOptions(dmo);
leadstoUpdate.add(l2);
}
}
}
if(!leadstoUpdate.isEmpty())
{
update leadstoUpdate;
}
}
The trigger works perfectly in the sandbox but I'm having problems with this class. I could be way off here as it fails and I can't get any coverage on the trigger yet.
}
ISRIds[i]= ISROpp[i].OwnerId;
}
ISR__c[] ISRObject = [select Id from ISR__c where Id IN: ISRIds];Map<String, ISR__c> lisrMap = new Map<String, ISR__c>();lisrMap.put(lISR.Id,lISR);
}
for (Lead lisr: ISROpp){if (!lisrMap.containskey(lisr.OwnerId)){lisr.GroupId =
'ISRsOrion';}
}
}
user u = [select Id from User where Id =:userInfo.getUserId()];string Eloqua = [
select Id from User where UserName = 'name@test.com'].Id;Lead l = [select Downloaded_Orion__c, Country, ID, LastModifiedById from Lead where LastModifiedById !=: Eloqua and LastModifiedById !=:u.Id limit 1];u.UserName =
'name@test.com';update u;l.Country =
'Canada'; update l;l.Downloaded_Orion__c = true;
update l;}
}
I'm in a situation where I do not have development help for this problem.
I have a set of leads that were loaded into a campaign using an incorrect lead assignment rule. I have corrected the rule and wish to run the existing group of leads through the corrected assignment rule.
How do I do this?
Thank you
@1t4scots - the only way to do this is to delete the leads & import them via the import wizard or data loader to create new leads.
This trigger worked great, but it doesn't seem to be kicking off the email set in the assignment rule. Did you find a way to get the Assignment rule to send the email? I can't use the SendEmail function in code, otherwise the Lead will get a copy of the email when I use the SetTargetObjectId.
Do you have the Apex Class you created for this trigger?
I don't have much apex coding experience, so please bear with me. I have the same issue.......trying to send Leads in a queue back through the Assignment rules once a field is updated. I have the code from this thread.....but not sure what to do with it. I put a name in for the trigger and save and the Database.DMLOptions dmo = new Database.DMLOptions(); dmo.assignmentRuleHeader.useDefaultRule = true; is what will make the assigment rules re-run?
Here is the test class for the Apex Class: https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod