I have created a custom account look up field on the lead object. I want to attach leads to existing accounts based on the company name.
So lets say if a lead comes-in and has filled out 'Salesforce' as the company name and an account already exists for Salesforce then the account look up field should automatically update to Salesforce.
The logic works like this:
1) Check if account name matching company name exists. 2) If yes, fetch account ID and map it to the account look up field on the lead. 3) Else, leave the field blank.
Here is the trigger I have been able to build so far after much research on the internet [still very new in writing triggers]:
Trigger Accname on Lead (before insert, before update) {
String leadaccountname;
String companyName;
for (Lead newLead : trigger.new){
companyName = newLead.Company;leadaccountname = newLead.Account_Leads__c;
break;
}
List<Account> lda = new List<Account>();
lda = [Select Id, Name from Account where Name =: companyName limit 1];
if (lda.size() > 0){
leadaccountname = lda[0].Name;
}
}
I don't get any errors when I run it but the account field is not updating automatically.