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
franklin_atxfranklin_atx 

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? 

Best Answer chosen by Admin (Salesforce Developers) 
franklin_atxfranklin_atx

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;
    }
  }

Message Edited by franklin_atx on 03-04-2010 07:14 AM

All Answers

jkucerajkucera

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); } }

 

 

 

franklin_atxfranklin_atx

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?

jkucerajkucera

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 } } }

 

 

 

franklin_atxfranklin_atx

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......

jkucerajkucera

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);
}
}

 


 

bethkbethk
Hi,  I am trying to do the same thing -- trigger active assignment rules when lead owner field has been updated to a particular queue.   I was thinking that the trigger event needed to be 'after update', not 'after insert', since the lead record already exists.  When I used the exact code above, with the only change being (after update), the trigger failed.  when I run the trigger with 'after insert' the active assigment rules do not fire.  Am I missing something?  thanks. 
jkucerajkucera

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:

 

 

trigger reassignLeads on Lead (after update){
List<Id> lIds=new List<id>();
For (lead l:trigger.new){
if (l.IsConverted==False){
lIds.add(l.Id);
}
}
if (AssignLeads.assignAlreadyCalled()==FALSE){
system.debug('Assign already called? '+AssignLeads.assignAlreadyCalled());
AssignLeads.Assign(lIds);
}
}

 

 

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. 

Message Edited by jkucera on 02-26-2010 06:08 PM
franklin_atxfranklin_atx

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;
    }
  }

Message Edited by franklin_atx on 03-04-2010 07:14 AM
This was selected as the best answer
dsfranklin_atxdsfranklin_atx

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.

 

public class LeadAssignment {

 

 

public static void SetISR(Lead[] ISROpp){

 

Map<Id,Group> QueueId= new Map<Id,Group>( [select id from group where name ='<name>' limit 1]);

}

 

string[] ISRIds = new string[ISROpp.Size()];

 

for(integer i =0 ; i< ISROpp.Size(); i++){

 

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>();

 

for(ISR__c lISR:ISRObject){

lisrMap.put(lISR.Id,lISR);

}

for (Lead lisr: ISROpp){

if (!lisrMap.containskey(lisr.OwnerId)){lisr.GroupId =

'ISRsOrion';

}

 

}

}

 

static testMethod void runtestTrigger(){

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;

 

}

}

1t4scots1t4scots

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

jkucerajkucera

@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.

Jeff LupJeff Lup

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.

SIB217SIB217

Do you have the Apex Class you created for this trigger?

SIB217SIB217

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?

 

 

 

 

 

Prerak Patel 1Prerak Patel 1
You can use Process builder to build out your re-assignment using @IsInvocableMethod now: https://automationchampion.com/2015/10/14/getting-started-with-process-builder-part-49-running-leads-assignment-rules-from-process-builder/
Here is the test class for the Apex Class: https://salesforce.stackexchange.com/questions/96131/how-to-write-a-test-class-for-an-invocablemethod
Sean FieldingSean Fielding
We have a free app for that on the app exchange.  https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000ErILmUAN
jackson carlosjackson carlos
I'm jackson carlos, the Assignment Maker brand representative. The company's objective is to assist students from all over the world with their assignments. We are now serving students in the United Arab Emirates, but our goal is to expand our services to include students from all around the world. We are now receiving more requests for essay writing services UAEStudents (https://uaestudents.ae/) are having difficulty finishing these assignments thus, our experienced writers are assisting and Advising them.
content writers.pkcontent writers.pk
I'm visiting your website for the first time, and I'm really interested in the posts you provide. Give me sufficient information. I appreciate you giving useful information, and please keep doing so: Content Writers Pk (https://contentwriters.com.pk/)
elai naoelai nao
Players will take on the role of a security guard. Based on the camera in the store, you will monitor and investigate any unusual things in the store. According to rumors, the former security man was murdered by the robot. Players will experience many strange things at the store. Five Nights at Freddy's (https://fivenightsatfreddys2.co).