• Jonathan Holloway
  • NEWBIE
  • 0 Points
  • Member since 2016

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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)