You need to sign in to do that
Don't have an account?
D-Vac
Apex to Assign Leads to Account Owners? Named Accounts
Does anyone have any Apex code (or ideas) that could be run prior to the web-to-lead assignment rules that would:
- Query for any accounts of the same name as the "Company Name" field in the lead
- Determine if it is a "named" account based on an account field
- Assign the lead to the account owner OR another designated user based on the account owner (like inside sales)
This would be really helpful for us in situations where we have named accounts that are outside the normal lead / territory assignments.
Thanks for any code, help or ideas!
Create a custom field on lead called Account__c (API name). Then something like this should work:
trigger addAccount on Lead (before Insert, before Update){ List<string> companies=new list<string>(); For (lead l:trigger.new){ companies.add(l.company); } List<Account> leadAccountIds=[Select Id, Name FROM Account WHERE Name IN: companies]; Map<String, Id> acctNameId=new Map<String, Id>(); For (Account a:leadAccountIds){ acctNameId.put(a.name,a.Id); } For (Lead l2:trigger.new){ if(acctNameId.containsKey(l2.company)){ l2.Account__c=acctNameId.get(l2.company); } } }
}
Nope-it shouldn't be a noticible performance hit. The big reason there are Apex governor limits are to prevent performance issues for customers. That means existing code tends to execute quickly.
One of my apps - the lead scoring app on the appexchange - can loop through 45,000 things and update 200 leads in under 10sec vs ~5 sec to mass update them without the 45,000 things to loop through.
For a trigger like this, it shouldn't take more than ~5% longer at most.
How can I refernece Account owner to be a lead owner in above code?
Force123-looking back at this code 1.5yrs later, I now see that it doesn't address the original question of assigning ownership, but rather connects the lead to the account so there's a related list of leads that show up on accounts with the same name as the company.
If you wanted to assign the owner, you'd grab it from the matching account and then update the lead owner with that userId. Assuming you still want the related list, this is a good start on a trigger for it:
Thanks John for the prompt response .
It didn't work. DO i need to add something in assignment rules?
John,
Worked like magic and it runs before assignment rules (or the direct assignment in the code over-rides the assignment rules) which is exactly what we needed. Anyway, thank you very much for posting this, it's much appreicated.
-Matt Stryker
you code works great. It also checks the case sensitiveness, how to make this case insensitive while comparing company name with account?
Thank you,
Lauren
Thanks,
Julien
1) Company name has to be an exact match (it is case sensitive)
2) Would need to put validation in place to prevent duplicate account names. Otherwise, it appears the trigger will assign any new or updated lead to the newest account record.