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
JN22JN22 

Convert Lead To Existing Account & Contact

Hello,

 

I am looking to develop a trigger that will fire when a Lead is created.  It will need to convert the lead to an Account and Contact (no Opportunity) but also search to see if there are existing Accounts/Contacts that it would be merged with.  The code I have below converts the leads, but it doesn't check for existing Accounts or Contacts.  Is there a way to modify it so that it will merge the Lead into any existing Accounts/Contacts or create new Accounts/Contacts if no existing ones are found?

 

trigger LeadConvert2 on Lead (after update) {
List<Account> accList = new List<Account>();
List<Contact> conList = new List<Contact>();
List<Opportunity> oppList = new List<Opportunity>();

//Bulkified
for (Lead lead : Trigger.new) {
if (lead.isConverted == false) //to prevent recursion
{

Database.LeadConvert lc = new Database.LeadConvert();
lc.setLeadId(lead.Id);

LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
lc.setConvertedStatus(convertStatus.MasterLabel);


Database.LeadConvertResult lcr = Database.convertLead(lc);
System.assert(lcr.isSuccess());


Account acc = new Account(id=lcr.AccountId);
accList.add(acc);

Contact con = new Contact(id=lcr.ContactId);
conList.add(con);

Opportunity opp = new Opportunity(id=lcr.OpportunityId);
oppList.add(opp);

}
}

update accList;
update conList;
update oppList;
}

Best Answer chosen by Admin (Salesforce Developers) 
JN22JN22

Just closing this thread.  After much searching and even checking out some Apps, it doesn't look like a good solutions exists for automatically converting leads, which is proably a best practice but does not help our project.  Thanks for your assistance.

All Answers

JN22JN22

Just closing this thread.  After much searching and even checking out some Apps, it doesn't look like a good solutions exists for automatically converting leads, which is proably a best practice but does not help our project.  Thanks for your assistance.

This was selected as the best answer