Lead conversion with following APEX code.

    Database.LeadConvert leadConvert;
    leadConvert = new database.LeadConvert();
    LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
    List<Database.LeadConvertResult> leadConversionResults = Database.convertLead( new list<database.LeadConvert>{leadConvert} );
    Lead leadData = [select id, status from lead where id = :leadId limit 1];
    leadData.status = 'Qualified';
    update leadData;


If the account and contact are already in SF then it is updating the already existing account and contact but the problem is it created new account and contacts as well. How can prevention of account and contact can be done?

1 Answer 1


To ensure you are updating existing Accounts/Contacts on Lead Conversion


Now, how you determine the acctIdToUse and contactIdToUse may require querying and is application-specific

Note that these three lines are superfluous as Lead.Status is set automatically by the leadConvert.setConvertedStatus(..) statement.

Lead leadData = [select id, status from lead where id = :leadId limit 1];
leadData.status = 'Qualified';
update leadData;
  • But when I use leadConvert.setAccountId(acctIdToUse); leadConvert.setContactId(contactIdToUse); I get error message some of the time "System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, cannot convert a lead with no company name into a business account: " Oct 13, 2020 at 9:34
  • That error message is not in all cases but in few even the company name is present. Oct 13, 2020 at 9:34
  • Lead.Company is a required field - you can't even save a Lead without it (unless you have enabled Person Accounts)
    – cropredy
    Oct 13, 2020 at 15:25
  • But in my case company name is present, it was never missing but for some cases the exception is weird, seems like I have this problem trailblazer.salesforce.com/ideaView?id=0873A000000cRaCQAU but via APEX. Oct 14, 2020 at 9:30
  • Hmm. So you have person accounts enabled?
    – cropredy
    Oct 15, 2020 at 6:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .