You need to sign in to do that
Don't have an account?
Jonathan 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)
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)
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: Hope this helps.
Kindly mark this as solved if the information was helpful.
Thanks,
Nagendra