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
Wendy Tanner 11Wendy Tanner 11 

Test Class for Lead Convert. Unable to push Apex Class to production

Hello! I have an apex class that was working great in sandbox. 
Using it in a process builder. Any one doing this, can you provide guidance? 


Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds)
    {
            Database.LeadConvert Leadconvert = new Database.LeadConvert();
            Leadconvert.setLeadId(LeadIds[0]);
            LeadStatus Leads= [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            Leadconvert.setConvertedStatus(Leads.MasterLabel);
           
            Database.LeadConvertResult Leadconverts = Database.convertLead(Leadconvert);
            System.assert(Leadconverts.isSuccess());
   }
}


When trying to push to prod, it states i need a test class. Tried the example below and i am not able to get it to work. Any input appreciated. When i run test it says lead already converted? 
@isTest 
      public class TestAutoConvertLeads{
      static testMethod void createnewlead() {
      User userToCreate = [Select id from user where profile.name='System Administrator' Limit 1];
      
      Test.startTest();    
      Lead leadToCreate =new Lead();
      List<id> Ids= New List<Id>();
      leadToCreate.ownerid= userToCreate.id;
      leadToCreate.LastName ='Test1';
      leadToCreate.LeadSource='Employee Referral';
      leadToCreate.Rating='';
      leadToCreate.Status='';
      insert leadToCreate; 
      
      Ids.add(leadToCreate.id);
      AutoConvertLeads.LeadAssign(Ids);
      
      Test.stopTest();
   }
}
 
Maharajan CMaharajan C
HI Wendy,

Please try the below changes you will get 100%.

In Lead object Company,Status are Mandatory Field. And no need of profile soql here.

@isTest 
public class TestAutoConvertLeads{
    static testMethod void createnewlead() {
        
        Lead leadToCreate =new Lead();
        List<id> Ids= New List<Id>();
        leadToCreate.LastName ='Test1';
        leadToCreate.Company ='Test Company';
        leadToCreate.LeadSource='Employee Referral';
        leadToCreate.Rating='Hot';                                   /// Mention the correct Rating as per your field values
        leadToCreate.Status='Open - Not Contacted';    /// Mention the correct status as per your field values

        insert leadToCreate; 
        
        Ids.add(leadToCreate.id);
        Test.startTest();    
        AutoConvertLeads.LeadAssign(Ids);
        Test.stopTest();
    }
}

Thanks,
Maharajan.C
Wendy Tanner 11Wendy Tanner 11
Thank You! Not sure if it matters, but i am trying to get it to convert to a person account and in that case I thought I could not have company name in there? (sorry if silly question)
I will try, and thank You for your help! 
 
Wendy Tanner 11Wendy Tanner 11
Getting this error if I put Company in there, maybe because i am not mapping it? System.DmlException: Insert failed. First exception on row 0; first error: CANNOT_EXECUTE_FLOW_TRIGGER, We can't save this record because the “Auto Convert Consumer Lead” process failed. Give your Salesforce admin these details. An Apex error occurred: System.DmlException: ConvertLead failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Your lead is missing a field mapping for the fields. For help, talk with your admin or see Map Custom Lead Fields for Lead Conversion (https://help.salesforce.com/articleView?id=customize_mapleads.htm" target="_blank) in Salesforce Help.: [] : [] Stack Trace Class.TestAutoConvertLeads.createnewlead: line 12, column 1 I can try to map it.. see if that makes a diff. WENDY TANNER Director of Operations Rollick, Inc. | O: 512-910-2408 | M: 480-258-1194 Website | LinkedIn | Facebook | Twitter | YouTube
Maharajan CMaharajan C
Check is there any fields are required to create the convesrion records( Account, Contact and Opportunity ).

For example in contact email is required then you have enter related lead mapped field while creating lead record in test class.

Thanks,
Maharajan.C
Wendy Tanner 11Wendy Tanner 11
ok, i think i am almost there now, but now getting an error about the lead convert status and I am not referencing Qualified anywhere in my process. System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Qualified: [Status] 
Stack Trace: Class.AutoConvertLeads.LeadAssign: line 11, column 1 Class.TestAutoConvertLeads.createnewlead: line 16, column 1

I feel like if i can solve for this i may be ok!