You need to sign in to do that
Don't have an account?
sam123456
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){
CompanyName.add(l.companyName);
CompanyState.add(l.State);
CompanyCountry.add(l.Country);
}
// 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]){
AccountOwnerMap.put(Acc.CompanyName+Acc.BillingState+Acc.BillingCountry,acc.ownerId);
}
// Iterate the lead and assign the owner id
for(Lead l : Trigger.New){
if(AccountOwnerMap.containskey(l.companyname+l.billingstate+l.billingcountry))
l.ownerId = AccountOwnerMap.get(l.companyname+l.billingstate+l.billingcountry);
}