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
D-VacD-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!

jkucerajkucera

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

 

 

}

D-VacD-Vac
John, thanks so much!  I will give this a try. 
D-VacD-Vac
John, do you think this type of query would lock things up when we try to create a bunch of new leads, through things like apex data loader, or the API - with something like a marketing platform like Eloqua - creating a couple hundred new leads from a spreadsheet upload?  
jkucerajkucera

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.

D-VacD-Vac
John, thanks so much for your reply.  Really appreciate the info.  Looking forward to testing it out.  
Force123Force123

How can I refernece Account owner to be a lead owner in above code?

jkucerajkucera

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:

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, OwnerId, Name FROM Account WHERE Name IN: companies];

Map<String, Id> acctNameId=new Map<String, Id>();
Map<String, Id> acctNameOwner=new Map<String, Id>();

For (Account a:leadAccountIds){
  acctNameId.put(a.name,a.Id);
  acctNameOwner.put(a.name,a.ownerId);
}

For (Lead l2:trigger.new){
  if(acctNameId.containsKey(l2.company)){
    l2.Account__c=acctNameId.get(l2.company);
    l2.ownerId=acctNameOwner.get(l2.company);
  }
}
}

 

Force123Force123

Thanks John for the prompt response .

 

It didn't work. DO i need to add something in assignment rules?

 

 

mms16mms16

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

Ramesh V.ax1832Ramesh V.ax1832
Hi jkucera,
you code works great. It also checks the case sensitiveness, how to make this case insensitive while comparing company name with account?
lmarshlmarsh
I'm hoping to use this but need a little more detail since I'm new to Dev.  Does anyone have finalized code that works in your org?  I'm still getting familiar with how the parts of Apex speak to each other and a working example would be a huge help.
Thank you,

Lauren
anne.clisham1.395887250069773E12anne.clisham1.395887250069773E12
I am using the Lead assignment code above.  What would I need to do if I only wanted to assign the Account Owner to the Lead for a specific profile?.
Vasbyt2Vasbyt2
Has anyone written a Test Class for this trigger or is there another quicker option ?   
PlainviewPlainview
This is great! How would one revise it to search for matching email domain names as opposed to company names?

Thanks,
Julien
Rob Morris (bostonmorris)Rob Morris (bostonmorris)
@jkucera can you do this by looking at email domain and website?
Joe Harper 19Joe Harper 19
@jkucera I added this code and it works perfect for matching on Accounts. Would you or anyone here be able to tell me how to modify or copy this to also match on Lead Company Name or Account / Lead phone number or Account / Lead email?  Thank you much.
Jacob Kohler 11Jacob Kohler 11
The trigger @jkucera provided above works wonderfully, but a few things to point out with its logic: 

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.