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
swain 10swain 10 

Test Class for apex class?

Hi Everyone,
Please check below apex class  How to get code coverage currently  I am getting 32%

Class:
public class OV_FlwLeadConversion {
    /**
* This method is used to Convert the Lead record to Account and contact
*/ 
    @InvocableMethod(label=' ' description='')
    public static List<String> convertLead(List<List<String>> configParams){
        List<String> finalResult = new List<String>();
        String status = 'Quick Entry Converted Successfully';
        try{
            String accountId=configParams[0][0];
            String contactId=configParams[0][1];
            String leadId=configParams[0][2];
            String acctName=configParams[0][3];
            String accountType=configParams[0][4];
            String city=configParams[0][5];
            String country=configParams[0][6];
            String street=configParams[0][7];
            String webSite=configParams[0][8];
            String recordTypeId=configParams[0][9];
           
            String firstName=configParams[0][10];
            String lastName=configParams[0][11];
            String title=configParams[0][12];
            String email=configParams[0][13];
            String role=configParams[0][15];
            String accPhone=configParams[0][16];
            String state=configParams[0][17];
            
            system.debug('--accountId---'+accountId);
            system.debug('--contactId---'+contactId);
            system.debug('--leadId---'+leadId);
            system.debug('--acctName---'+acctName);
            system.debug('--accountType---'+accountType);
            system.debug('--city---'+city);
            system.debug('--country---'+country);
            system.debug('--street---'+street);
            system.debug('--webSite---'+webSite);
            system.debug('--recordTypeId---'+recordTypeId);
            system.debug('--firstName---'+firstName);
            system.debug('--lastName---'+lastName);
            system.debug('--title---'+title);
            system.debug('--email---'+email);
            
            String conRecordType;
            Id conRecordTypeId;
            system.debug('--accountType--:'+accountType);
            system.debug('--configParams[0][4]--:'+configParams[0][4]);
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(leadId);
            lc.setDoNotCreateOpportunity(true);
            if(accountId != ''){
                lc.setAccountId(accountId); 
            }
            if(contactId != '' ){
                lc.setContactId(contactId);                                        
            } 
            //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            //lc.setConvertedStatus(convertStatus.MasterLabel);//Qualified
            lc.setConvertedStatus('Qualified');
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.debug('--------``````----accountId------'+accountId);
            System.debug('--------`````----contactId------'+contactId);
            if(lcr.isSuccess()){
                system.debug('-------isSuccess----------');
                if((accountId == ''|| accountId==null) && (contactId=='' ||contactId == null)){ 
                    String recordTypeIds;
                    recordTypeIds = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get(accountType).getRecordTypeId(); 
                    system.debug('--recordTypeIdssssssssssssssssssssssssss---'+recordTypeIds);
                    Account acct = new Account();
                    acct.Id=lcr.getAccountId();
                    acct.name=acctName;
                    acct.BillingCountry=country;
                    acct.Website=webSite;
                    acct.BillingCity=city;
                    acct.BillingStreet=street;
                    acct.Type=accountType;
                    acct.RecordTypeId=recordTypeIds;
                    acct.Customer_Category__c='Unspecified';
                    acct.Phone=accPhone;
                    acct.BillingState=state;
                    
                    upsert acct;
                }
                // This needs to be removed and put it in Config
                if(accountType == 'Brokerage'){
                    conRecordType = 'Broker';
                }else if(accountType == 'Prologis'){
                    conRecordType = 'Employee';
                }else if(accountType == 'Venture Account'){
                    conRecordType = 'Venture Contact' ;
                }else if(accountType == 'Other'){
                    conRecordType = 'Non Ventures' ;   
                }else{
                    conRecordType = accountType;
                }
                if(accountType!=null && accountType!=''){
                    conRecordTypeId = Schema.getGlobalDescribe().get('Contact').getDescribe().getRecordTypeInfosByName().get(conRecordType).getRecordTypeId(); 
                    system.debug('-------conRecordTypeId----------'+conRecordTypeId);
                } 
             
                if(contactId == ''|| contactId == null){
                    Contact con = new Contact();
                    con.Id=lcr.getContactId();
                    con.FirstName = firstName;
                    con.LastName = lastName;
                    con.RecordTypeId=conRecordTypeId;
                    con.Title = title;
                    con.Email=email;
                    con.Role__c=role;
                    upsert con;
                }
            }else{
            //if(!lcr.isSuccess()){
                status='Quick Entry Conversion Failed';
            }
            finalResult.add(status);            
        }
        catch(Exception ex){
            status = ex.getMessage();
            finalResult.add(status);
            system.debug('-----Exception'+ex.getStackTraceString());
            return finalResult;
        }
        return finalResult;
    }
    
}

Test Class:
@isTest(SeeAllData=true)
private class OV_FlwLeadConversionTest {
    @isTest static void testLeadConvert(){
        Lead ld= new Lead();
        ld.LastName = 'abc';
        ld.Company = 'xyz';
        ld.FirstName = 'fd';
        ld.Email = 'ab@fr.com';
        ld.Status='Qualified';
        insert ld;
        String status = 'Lead Converted Successfully';
        String conRecordType;
        Id conRecordTypeId;
        List<List<String>> configParams = new List<List<String>>();
        List<String> configParam = new List<String>();
        configParam.add('');
        configParam.add('');
        configParam.add(ld.Id);
        configParam.add('xyGroups');
        configParam.add('Brokerage');
        configParam.add('US');
        configParam.add('India');
        configParam.add('Aurora');
        configParam.add('');
        configParam.add('');
        configParam.add(ld.FirstName);
        configParam.add(ld.LastName);
        configParam.add('Master');
        configParam.add(ld.Email);
        configParam.add('Broker');
        configParam.add('368638');
        configParam.add('Karnataka');
        configParams.add(configParam);
        
        List<String> finalResult = OV_FlwLeadConversion.convertLead(configParams);
        system.assert(finalResult.size() > 0);   
        
  
    }

}



Thanks
Steven NsubugaSteven Nsubuga
In your class, you specified a wrong index 17, and that caused an exception to be thrown.
public class OV_FlwLeadConversion {
    /**
* This method is used to Convert the Lead record to Account and contact
*/ 
    @InvocableMethod(label=' ' description='')
    public static List<String> convertLead(List<List<String>> configParams){
        List<String> finalResult = new List<String>();
        String status = 'Quick Entry Converted Successfully';
        try{
            String accountId=configParams[0][0];
            String contactId=configParams[0][1];
            String leadId=configParams[0][2];
            String acctName=configParams[0][3];
            String accountType=configParams[0][4];
            String city=configParams[0][5];
            String country=configParams[0][6];
            String street=configParams[0][7];
            String webSite=configParams[0][8];
            String recordTypeId=configParams[0][9];
           
            String firstName=configParams[0][10];
            String lastName=configParams[0][11];
            String title=configParams[0][12];
            String email=configParams[0][13];
            String role=configParams[0][14];
            String accPhone=configParams[0][15];
            String state=configParams[0][16];
            
            system.debug('--accountId---'+accountId);
            system.debug('--contactId---'+contactId);
            system.debug('--leadId---'+leadId);
            system.debug('--acctName---'+acctName);
            system.debug('--accountType---'+accountType);
            system.debug('--city---'+city);
            system.debug('--country---'+country);
            system.debug('--street---'+street);
            system.debug('--webSite---'+webSite);
            system.debug('--recordTypeId---'+recordTypeId);
            system.debug('--firstName---'+firstName);
            system.debug('--lastName---'+lastName);
            system.debug('--title---'+title);
            system.debug('--email---'+email);
            
            String conRecordType;
            Id conRecordTypeId;
            system.debug('--accountType--:'+accountType);
            system.debug('--configParams[0][4]--:'+configParams[0][4]);
            Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(leadId);
            lc.setDoNotCreateOpportunity(true);
            if(accountId != ''){
                lc.setAccountId(accountId); 
            }
            if(contactId != '' ){
                lc.setContactId(contactId);                                        
            } 
            //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            //lc.setConvertedStatus(convertStatus.MasterLabel);//Qualified
            lc.setConvertedStatus('Qualified');
            Database.LeadConvertResult lcr = Database.convertLead(lc);
            System.debug('--------``````----accountId------'+accountId);
            System.debug('--------`````----contactId------'+contactId);
            if(lcr.isSuccess()){
                system.debug('-------isSuccess----------');
                if((accountId == ''|| accountId==null) && (contactId=='' ||contactId == null)){ 
                    String recordTypeIds;
                    recordTypeIds = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get(accountType).getRecordTypeId(); 
                    system.debug('--recordTypeIdssssssssssssssssssssssssss---'+recordTypeIds);
                    Account acct = new Account();
                    acct.Id=lcr.getAccountId();
                    acct.name=acctName;
                    acct.BillingCountry=country;
                    acct.Website=webSite;
                    acct.BillingCity=city;
                    acct.BillingStreet=street;
                    acct.Type=accountType;
                    acct.RecordTypeId=recordTypeIds;
                    acct.Customer_Category__c='Unspecified';
                    acct.Phone=accPhone;
                    acct.BillingState=state;
                    
                    upsert acct;
                }
                // This needs to be removed and put it in Config
                if(accountType == 'Brokerage'){
                    conRecordType = 'Broker';
                }else if(accountType == 'Prologis'){
                    conRecordType = 'Employee';
                }else if(accountType == 'Venture Account'){
                    conRecordType = 'Venture Contact' ;
                }else if(accountType == 'Other'){
                    conRecordType = 'Non Ventures' ;   
                }else{
                    conRecordType = accountType;
                }
                if(accountType!=null && accountType!=''){
                    conRecordTypeId = Schema.getGlobalDescribe().get('Contact').getDescribe().getRecordTypeInfosByName().get(conRecordType).getRecordTypeId(); 
                    system.debug('-------conRecordTypeId----------'+conRecordTypeId);
                } 
             
                if(contactId == ''|| contactId == null){
                    Contact con = new Contact();
                    con.Id=lcr.getContactId();
                    con.FirstName = firstName;
                    con.LastName = lastName;
                    con.RecordTypeId=conRecordTypeId;
                    con.Title = title;
                    con.Email=email;
                    con.Role__c=role;
                    upsert con;
                }
            }else{
            //if(!lcr.isSuccess()){
                status='Quick Entry Conversion Failed';
            }
            finalResult.add(status);            
        }
        catch(Exception ex){
            status = ex.getMessage();
            finalResult.add(status);
            system.debug('-----Exception'+ex.getStackTraceString());
            return finalResult;
        }
        return finalResult;
    }
    
}