You need to sign in to do that
Don't have an account?
![Luke@PCA Luke@PCA](https://dfc-org-production.my.site.com/img/userprofile/default_profile_45_v2.png)
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?
something like this
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.
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.