1

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 Existing 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 from I want it to convert to.

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).
2

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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.