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
Brian Good 13Brian Good 13 

Passing fields into Lead Conversion Apex Class

Hello,

I'm utilizing the Database.LeadConvert class invoked from a flow to auto convert lead records.  I also want to set the AccountId and ContactId (shows in bold/italic below as the variables ExistingAccountID and ExistingContactId) from fields I am populating on the Lead record.  I'm not quite sure how to select and declare them in the class.  I am only converting one record at a time and I will be passing the ID variable (LeadIds) from a Flow to the Apex class, not sure if that makes a difference as this looks like a mass convert for multiple Lead records.  This is the method I'm using from Automation Champion:

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.setAccountId(ExistingAccountId);
Leadconvert.setContactId(ExistingContactId);

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);
}
}
}
mukesh guptamukesh gupta
Hi Brain ,

Please follow this code:
 
Set<String> setCompany = new Set<String>();
for(Lead lead : Trigger.New)
{
  setCompany.add(lead.Company);
}

Map<String,Account> mapAccounts = new Map<String,Account>();
For(Account acc : [SELECT Id, Name FROM Account WHERE Name IN : setCompany])
{
  mapAccounts.put(acc.Name,acc);
}

List<Database.LeadConvert> lstLC = new List<Database.LeadConvert>();
For(Lead lead : Trigger.New)
{
  Account matchingAccount = mapAccounts.get(lead.Company);
  if(matchingAccount != null)
  {
    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(lead.id);
    lc.setAccountId(matchingAccount.Id); // Setting existing Account Id for converting lead to that account
    lstLC.add(lc);
    setAccountId.add(matchingAccount.Id);
  }
}
    
LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
for(Database.LeadConvert lc : lstLC)
{
  lc.setConvertedStatus(convertStatus.MasterLabel);
}
List<Database.LeadConvertResult> lcr = Database.convertLead(lstLC);

if you need any assistanse, Please let me know!!


Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
Brian Good 13Brian Good 13
Maybe I am just not understanding.  When I pass the Lead record into my Apex class cannot I not just query the fields (fields on Lead record - Existing Account__c = AccountID and Converted_Contact__c = Contact ID) I am setting on the Lead record as a value and set my variable as that?  
Brian Good 13Brian Good 13
So within my flow I am setting the existing fields with the correct Account and Contact IDs.  So I just want to reference the values.  I don't want to have to search for company / contact by name because I already have the ID.
Valentin ROLLAND 16Valentin ROLLAND 16

Hello @Brian Good 13

I'm in the exactly same situation. Have you found a solution? 

Thanks

Brian Good 13Brian Good 13
@Valentin ROLLAND 16
Here is the code that works for me.  I created custom fields to hold the existing Account and Contact IDs.

Public class AutoConvertLead
{
    @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, Existing_Contact_Id_for_Conversion__c, Existing_Account_Id_for_Conversion__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.setContactId(leadMap.get(currentlead).Existing_Contact_Id_for_Conversion__c);
              Leadconvert.setAccountId(leadMap.get(currentlead).Existing_Account_Id_for_Conversion__c);

            Leadconvert.setDoNotCreateOpportunity(TRUE);// 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);
        }
    }
}