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

trigger to assign lead to account owner

i want a trigger that assigns a lead to account owner whenever company field of lead matches with the account name and also lead address matches with the account address 

//do this in before trigger (Before Insert)
Set<String> CompanyName = new Set<String>();
Set<String> CompanyState = New Set<String>();
Set<String> CompanyCountry = New Set<String>();

//Collect the company name , state and country values in to a set.
for(Lead l : Trigger.New){

// form a map key = account company + account state + account country - value = account owner id
Map<String,String> AccountOwnerMap = new Map<String,string>();
for(Account Acc :[Select ownerId,billingState,BillingCountry,CompanyName from Account where CompanyName in: CompanyName
                           and BillingState in: CompanyState and BillingCountry in: CompanyCountry]){

// Iterate the lead and assign the owner id
for(Lead l : Trigger.New){
           l.ownerId = AccountOwnerMap.get(l.companyname+l.billingstate+l.billingcountry);