+ Start a Discussion
Tanmay SahaiTanmay Sahai 

Some tweaks in the existing code to meet the requirement

Hi All,
Below is my Apex Code which I have created to auotmate Lead Conversion to Account and Contact:

Public class AutoConvertLeads
{
    @InvocableMethod //Used to invoke this Class in Process Builder
    public static void LeadAssign(List<Id> LeadIds)
    {
     map< id,id> mapAccount = new map<id,id>();
     {
       for(Account a : [SELECT id,Company_Name_Custom__c from Account])
        {
         mapAccount.put(a.Company_Name_Custom__c, a.id);
        }

        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(Lead currentlead: [SELECT Id,Company_Name_Custom__c from Lead where Id in:LeadIds ])
     {
         if( !mapAccount.isEmpty() && mapAccount.ContainsKey(currentlead.Company_Name_Custom__c) )
         {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.Id);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                Leadconvert.setAccountId(mapAccount.get(currentlead.Company_Name_Custom__c));
                MassLeadconvert.add(Leadconvert);
             
         }
         else
         {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.Id);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                MassLeadconvert.add(Leadconvert);
         }
      }

        
        if (!MassLeadconvert.isEmpty())
        {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }   }    
}

The code is working fine but I have 1 problem with it:

The converted contact is always attaching to the Same account no matter which account name you put in the company for the lead conversion.

Below is my requirement:
" Our leads flow into Salesforce from Admin Panel/HubSpot. Custom Company Name is a common Lead field for both HubSpot and Salesforce. When the Lead is created by flowing into Salesforce, the standard Company field of Lead is blank or [not provided].

We also have a check box on the Lead named: Created in Panel.


What we want is to convert only those leads which have this Created in Admin Panel checkbox true. Also, on conversion, the value in the Custom Company Name should overwrite the Company of the Lead thereby replacing it as Account Name on the Converted Contact.

Then, if the Account already exists, it should only create a contact and attach to the existing Account.
"

Can anyone of you help me with tweaking the code to meet my above requirement.

Appreciate your help and prompt response.

Thanks!
TS
Dhanraj Poojary 18Dhanraj Poojary 18
Can you get the Complete Lead object instead of just Lead Id in the Method Signature 
public static void LeadAssign(List<Id> LeadIds)
Then it could serve your purpose as the List<Lead> will consist the Created in Panel value.
Based on that you can perform your further processing
Tanmay SahaiTanmay Sahai
Hi Dhanraj,

Thanks for your response. I have made use of the Lead Sobject instead of Id for mapping but it is still asking to put Id in the Company Name Custom when creating a Lead record. Can you please help me with the code??

Currently the value in the Company Name Custom is ID and I want to replace it with a Name. How can I achieve the same??

Await your response.

Thanks!
Dhanraj Poojary 18Dhanraj Poojary 18
The reason it is asking to put ID is because of the way you have defined your MAP
map< id,id> mapAccount = new map<id,id>();
You Probably need MAP<ID,String>
Tanmay SahaiTanmay Sahai
Thanks. I got to know what mistake I was doing. Mapping string resolved the issue.

 
Dhanraj Poojary 18Dhanraj Poojary 18
Dats Great to know. Happy Coding. Please mark it as best answer if it resolved your query.