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
Luke@PCALuke@PCA 

Lead Assignment Trigger

I have a quite complex lead assignment process for which lead assignment rules are simply not up to the task, both in complexity and number.

I can overcome the complexity problem by using a webservice to calculate the name of the user I need to assign the lead to but I still have problems as rules like Manager__c = "X" assign to user X would cause me to hit the limit of assignment rules.

So I was thinking I could do it with an Apex trigger but I have been unable to find a way so far. I have some up with something like:

Code:
trigger assignLead on Lead (before insert, before update) 
{ 
 for (Integer i = 0; i < Trigger.new.size(); i++)
 {
  if (trigger.new[i].Status == 'Qualified')   
  {   
   Trigger.new[i].Owner.Name = [Select Name from User where Name = 'X'][0].Name;  
  } 
 }
}

Messy I know but that was after trying many different things, so far but that throws a compile error "Field is not writeable: Owner.Name at line 7 column 28" and was wondering if I was thinking down the wrong route?
 


 

Ron HessRon Hess
to change the owner, you find the correct new owner ID, then write that ID into the ownerID field of the lead.

something like this
Trigger.new[i].Ownerid = [Select id from User where Name = 'Luke Sparey' limit 1].id;

Luke@PCALuke@PCA
That got it, many thanks Ron :)
Ron HessRon Hess
note: this trigger will fail when given a batch or bulk upload of records.  the trigger will get 200 rows and your code will try to make 200 SOQL queries , failing at ~21

rather, you should bulk-ify your code to make a single account query to get all the info you need , then make all the updates needed.

so, two loops

first loop gathers the account id's needed

then an account soql query using IN :myAccountIdList

then another loop to merge the results of the account query with the trigger.new array.


Luke@PCALuke@PCA
Thanks again Ron, code is now:
 
Code:
trigger assignLead on Lead (before insert, before update)
{
List<String> userNameList = new List<String>();

for (Integer i = 0; i < Trigger.new.size(); i++)
{
userNameList.add(Trigger.new[i].Manager__c);
}

List<User> userIdList = new List<User>([Select id from User where Name in :userNameList]);

for (Integer i = 0; i < Trigger.new.size(); i++)
{
if (trigger.new[i].Status == 'Qualified')
{
Trigger.new[i].Ownerid = userIdList[i].id;
}
}
}

 
Ron HessRon Hess
be careful that the
userIdList

is the exact same order as the trigger.new list, i'm not sure you can assume this is always true.

you may have to add a lookup function so that you can get the
userID out of the userIDList based on the name

I'm not sure of this, but i would not depend on the order of items in the
userIdList without testing it, what if the same user was listed twice in the ID list, perhaps it would only be returned once, thus making userIDlist shorter than trigger.new list.