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
PhoenixRising12PhoenixRising12 

Writing a Test Class for Lead Conversion

I have a class to convert leads and I'm trying to write a test class for this so I can deploy to production, how should I approach this? 

Global class AutoConvertLeads {
    @InvocableMethod
    public static void LeadAssign(List<Id> LeadIds){

        List<String> lstNames = new List<String>();
        for (User u : [SELECT Id FROM User WHERE Name = 'XXX' LIMIT 1]){
            
            lstNames.add(u.Id);
        }
        
    String Id = lstNames[0];
        
    Database.LeadConvert[] converts = new Database.LeadConvert[0];
    LeadStatus convertedStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted = TRUE LIMIT 1];
    for(Lead record: [SELECT Id FROM Lead WHERE IsConverted = false LIMIT 1000]) { 
    Database.LeadConvert convert = new Database.LeadConvert();
    convert.setLeadId(record.Id); 
    convert.setConvertedStatus(convertedStatus.MasterLabel);
    convert.setOwnerId(XXX);
    convert.setDoNotCreateOpportunity(TRUE);
    converts.add(convert);
}
    
        if(!converts.isEmpty()){
            
            Database.convertLead(converts);
            
        }

        
       
      Integer listsize = converts.Size();
       DateTime NOW = system.NOW();
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        String[] toAddresses = new String[] {''};
        mail.setToAddresses(toAddresses);
        mail.setSubject('Leads have been Converted, ' + NOW);
        mail.setPlainTextBody('Dear Admins, ' + listsize + ' Lead(s) were just converted into Person Accounts');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

    }
  
}
Best Answer chosen by PhoenixRising12
AnkaiahAnkaiah (Salesforce Developers) 
Hi Phoenix,

Try with below code.
@isTest 
      public class TestAutoConvertLeads{
      static testMethod void createnewlead() {
      User userToCreate = [Select id from user where profile.name='System Administrator' AND IsActive=TRUE Limit 1];
      system.debug('userToCreate=='+userToCreate);
     // Test.startTest();    
      Lead leadToCreate =new Lead();
      List<id> Ids= New List<Id>();
      leadToCreate.ownerid= userToCreate.id;
      leadToCreate.LastName ='Gupta';
      leadToCreate.Company='Salesforce';
      leadToCreate.LeadSource='Employee Referral';
      leadToCreate.Rating='';
      leadToCreate.Status='';
      //leadToCreate.Email = 'ankaiah_b@yahoo.com';
      insert leadToCreate; 
      system.debug('leadToCreate=='+leadToCreate);
      
      Ids.add(leadToCreate.id);
      AutoConvertLeads.LeadAssign(Ids);
      
      //Test.stopTest();
   }
}
If this helps, Please mark it as best answer.

Thanks!!
 

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Phoenix,

Try with below code.
@isTest 
      public class TestAutoConvertLeads{
      static testMethod void createnewlead() {
      User userToCreate = [Select id from user where profile.name='System Administrator' AND IsActive=TRUE Limit 1];
      system.debug('userToCreate=='+userToCreate);
     // Test.startTest();    
      Lead leadToCreate =new Lead();
      List<id> Ids= New List<Id>();
      leadToCreate.ownerid= userToCreate.id;
      leadToCreate.LastName ='Gupta';
      leadToCreate.Company='Salesforce';
      leadToCreate.LeadSource='Employee Referral';
      leadToCreate.Rating='';
      leadToCreate.Status='';
      //leadToCreate.Email = 'ankaiah_b@yahoo.com';
      insert leadToCreate; 
      system.debug('leadToCreate=='+leadToCreate);
      
      Ids.add(leadToCreate.id);
      AutoConvertLeads.LeadAssign(Ids);
      
      //Test.stopTest();
   }
}
If this helps, Please mark it as best answer.

Thanks!!
 
This was selected as the best answer
PhoenixRising12PhoenixRising12
It worked!! thank you so much. Greatly appreciated!