You need to sign in to do that
Don't have an account?
Brian 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);
}
}
}
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);
}
}
}
Please follow this code:
if you need any assistanse, Please let me know!!
Kindly mark my solution as the best answer if it helps you.
Thanks
Mukesh
Hello @Brian Good 13
I'm in the exactly same situation. Have you found a solution?
Thanks
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);
}
}
}