You need to sign in to do that
Don't have an account?
Shawn Reichner 29
Lead Conversion Via Trigger Issues - Please Help
Awesome Devs!
I have the followign trigger that will fire upon a lead creation to auto convert the lead. That is working like a charm, however the issue I am running into is doing a search for an existing account to append the Contact and Opportunity to that is created via the lead conversion.
I have some logic built in that looks like it is not working to locate an existing account based on the Lead Company field, but my code is still creating a new account each time a lead is automaticlaly converted.
Can anyone help with getting my code to properly append the lead to the existing account if one exists, and create a new one if one is not found?
Here is the trigger code:
I have the followign trigger that will fire upon a lead creation to auto convert the lead. That is working like a charm, however the issue I am running into is doing a search for an existing account to append the Contact and Opportunity to that is created via the lead conversion.
I have some logic built in that looks like it is not working to locate an existing account based on the Lead Company field, but my code is still creating a new account each time a lead is automaticlaly converted.
Can anyone help with getting my code to properly append the lead to the existing account if one exists, and create a new one if one is not found?
Here is the trigger code:
Trigger AutoConverter on Lead (after insert) { LeadStatus convertStatus = [ select MasterLabel from LeadStatus where IsConverted = true limit 1 ]; List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>(); MAP<Id,Account> AccountSearch = new MAP<Id,Account>([SELECT ID, Name FROM Account]); for (Lead lead: Trigger.new) { if (!lead.isConverted && lead.Company != null) { If(AccountSearch.equals(Lead.Company)){ Account Acct = AccountSearch.get(Lead.Company); ID AcctId = Acct.Id; Database.LeadConvert lc = new Database.LeadConvert(); String oppName = lead.Company+Lead.CreatedDate; lc.setLeadId(lead.Id); lc.setOpportunityName(oppName); lc.setConvertedStatus(convertStatus.MasterLabel); lc.setAccountId(AcctId); leadConverts.add(lc); } Else{ Database.LeadConvert lc = new Database.LeadConvert(); String oppName = lead.Name; lc.setLeadId(lead.Id); lc.setOpportunityName(oppName); lc.setConvertedStatus(convertStatus.MasterLabel); leadConverts.add(lc); } } } if (!leadConverts.isEmpty()) { List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts); } }
All Answers
Thank you so very much sir! That worked like a charm!