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
Dorel4Dorel4 

Apex Trigger for Lead Assignment help

I need help creating an Apex trigger to get the lead assignment rule to trigger on our imported leads. I need it to run on all leads and I am not sure how to get it to work.  thank you

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

}
  
Best Answer chosen by Dorel4
Dorel4Dorel4
I recreatede the Lead Assignment Rules in Process Builder and it worked!

All Answers

Peter Kalina IBMPeter Kalina IBM
Hi Dorel4,

can you give me more information, from which view/screen you will be importing these date. It is Data Loader or Import Wizard? You will be importing or updating too?

As first i do not know unswer to your issue i saw couple of change that i will do.
1. You have parameter after update, but you mentioned that you will import data
2. If you have more than 50000 data then your calling will exiced government limits, in this case you can use batch jobs.
Dorel4Dorel4
Hi Peter,

Thank you for looking at my problem.  We are importing leads using Apex Code, written by a consultant.  Currently none of these leads are going into the queues created by our lead assignment rules.  The rules only trigger when the leads are manually entered.   I am fairly new at writting code and followed some of the posts I found.  I am not sure how to fix this, so our users can process leads using the queue.
Peter Kalina IBMPeter Kalina IBM
hi Dorel4,

oks so as i understand you have your custom Visualforce page and in behind some logic in apex classes. When you are inserting data you do not have validation check.

Second case is that if you are inserting data manually your validation rule is turn on and check data automatically.

In this case you can only rebuild their apex class and add there your condition.

Paste me their apex class that appear to your visual force page.

Thank you
Dorel4Dorel4
Is there a way to do it in a seporate trigger, it is a live system?  We currently have a manual work around to distibute the leads. 
 
Peter Kalina IBMPeter Kalina IBM
Yes there is a way. Can you exactly describe me what you what like to update or check after you insert Lead records?
or what the existing validation rules is doing.

Thank you
Dorel4Dorel4
Thank you.  We have a Lead Assignment Rule assigning the leads into 4 queues. The rules are based on internal codes we added to each lead before it is imported.  The problem we are having is triggering the assignment rules when the leads are imported via the apex code. We have two validation rules based on manual entered leads.  Do I need to recreate the rules in the trigger? 
Dorel4Dorel4
I recreatede the Lead Assignment Rules in Process Builder and it worked!
This was selected as the best answer