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
Jonathan HollowayJonathan Holloway 

@InvocableMethod Apex Class to AutoConvertLead, but using Existing Contact ID from a custom field within same Lead record

Hi, can anyone kindly help with amending the below code so that it can convert the Lead to an existing Contact (and contact's Account) by using the specified Exisitng Contact ID which would be stored on the same Lead record in a custom field.

I'm hoping to trigger this Apex Class from Process Builder (or Flows), but I believe the only piece I'm missing and struggling with is knowing how to write the code so that I can pass the contact id I want it to convert to.

Thank you kindly in advance,
Jonathan


Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion 
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}


I think I need to get this type of ttext into the code but not confident at all how to do it;

- 'Leadconvert.setContactId(contactId)'
- 'Leadconvert.setAccountId(accountId)' or better still may be something like 'Leadconvert.setAccountId(contactId.accountId)' (my logic being to avoid errors where the account id for the contact possibly changes over time)
 
NagendraNagendra (Salesforce Developers) 
Hi Jonathan,

Since you can only pass in the list of Ids, you'll need to query the database to return the leads you're working with so that you can access other fields on those. You'll also want to create a Map<Id,Lead>as well to make it easier on yourself. Something like the following should get you pretty close:
Public class AutoConvertLeads{

    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
        Map<Id,Lead> leadMap = new Map<Id,Lead>([SELECT Id, Your_Contact_Id_Field__c, ... FROM Lead WHERE Id = :LeadIds]);
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds){
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE); //Remove this line if you want to create an opportunity from Lead Conversion
                Leadconvert.setContactId(leadMap.get(currentlead).Your_Contact_Id_Field__c);
                //set any other fields you need
                MassLeadconvert.add(Leadconvert);
        }

        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}
Hope this helps.

Kindly mark this as solved if the information was helpful.

Thanks,
Nagendra