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
Tanmay SahaiTanmay Sahai 

Compilation error

Hello all,

I have created an Apex Class to convert leads into Account and Contact and have also put the account duplication check. It is using Invocable method as it is being used in a PB. But my code is not compiling although I have all the custom fields available and have checked the FLS as well.

Below is the code:

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds){}
    public static map< id,id> mapAccount = new map<id,id>();
    {
    for(Account a : [select id from Account])
{
     mapAccount.put(a.id, a.id);
}

    
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds)
     {
         if(mapAccount.get(currentlead.Company_Name_Custom__c ) != NULL)
         {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                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);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                MassLeadconvert.add(Leadconvert);
         }
      }

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

   Here is the screenshot of the errors:
User-added image
Can any one of you help me with the improved code.

My scenario is:
" Our leads flows from third party apps (like chartio/hubspot) to Salesforce. We have a check box which shows whether the Lead came from these apps or were created manually. We want to automate the Lead Conversion on the basis of this Checkbox(True: Convert; False: Don't convert). I have implemented this requirement using an Apex Class and PB. However, this resulted in Duplicate Account Creation and sometimes Contact created without Account. "

The Name of the Company maps to the Company Name Custom (API Name: Company_Name_Custom__c) when the Lead flows to Salesforce. But when converting lead, it is taking the default Lead Standard field: Company and thus resulting in duplicate and/or blank accounts. I have used the custom field in my logic but still getting an error.

As per our lead conversion process, a lead is converted to An Account (the name of the company) and  contact(name of the lead) for the first time. Then if we want to create a new contact for the existing account, we again create a new lead  and select the existing Account as company and convert that lead into contact and the contact gets attached to the Account.

Look forward for your suggestions/responses.
Thanks!
 
Akshay_DhimanAkshay_Dhiman
Hi Tanmay Sahai,

this is code for Lead conversion to Account and Contact
 
public with sharing class LeadConversion {
 public static void ConvertingLead(List<Lead> leadList){ // pass the list of Lead to convert 
  LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true Limit 1];
  List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
  for(Lead currentlead: leadList){
   Database.LeadConvert Leadconvert = new Database.LeadConvert();
   Leadconvert.setLeadId(currentlead.Id); 
   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);
  }
  }
}

Tanmay there are many mistake in your code I changed try it .

Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds){}
    public static map< id,id> mapAccount = new map<id,id>();
    {
    for(Account a : [select id,Company_Name_Custom__c from Account])
{
     mapAccount.put(a.id, a);
}

    
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        for(id currentlead: LeadIds)
     {
         if(mapAccount.get(currentlead).Company_Name_Custom__c  != NULL)
         {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead);                
                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);                
                Leadconvert.setConvertedStatus(CLeadStatus.MasterLabel);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                MassLeadconvert.add(Leadconvert);
         }
      }

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


Please let me know if you have any query.

Thanks,

Akshay

Tanmay SahaiTanmay Sahai
Hello Akshay,

Thanks so much for your response. I tried your code but I am still getting the same Compile error. Below is the screenshot for the same:
User-added image

Can you check and help me with the same.

Thanks!
Akshay_DhimanAkshay_Dhiman
Tanmay Can you explain your problem?
Tanmay SahaiTanmay Sahai
Hi,

I have copy pasted your code and am still getting the same Compile error in my developer console. The compilation errors that I get is shared via screenshot below:

User-added image

Regarding my requirement, below are the details:

" Our leads flows from third party apps (like chartio/hubspot) to Salesforce. We have a check box which shows whether the Lead came from these apps or were created manually. We want to automate the Lead Conversion on the basis of this Checkbox(True: Convert; False: Don't convert). I have implemented this requirement using an Apex Class and PB. However, this resulted in Duplicate Account Creation and sometimes Contact created without Account. "

The Name of the Company maps to the Company Name Custom (API Name: Company_Name_Custom__c) when the Lead flows to Salesforce. But when converting lead, it is taking the default Lead Standard field: Company and thus resulting in duplicate and/or blank accounts. I have used the custom field in my logic but still getting an error.

As per our lead conversion process, a lead is converted to An Account (the name of the company) and  contact(name of the lead) for the first time. Then if we want to create a new contact for the existing account, we again create a new lead  and select the existing Account as company and convert that lead into contact and the contact gets attached to the Account.


Please let me know if you need any additional information.

Thanks!
 
Tanmay SahaiTanmay Sahai
Hi Akshay,

Did you understand my problem statement?Can you help me with the code??

Await your response.

Thanks in advance!

TS