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 

Lead conversion Test Class

Apex class:- I am getting only 30%code coverage, Please review what and where i am doing mistake?
public class OV_FlwLeadConversion {
  
    @InvocableMethod(label=' ' description='')
    public static List<String> convertLead(List<List<String>> configParams){
        List<String> finalResult = new List<String>();
        String status = 'Lead 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];  
            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 == ''&& contactId==''){ 
                    
                    recordTypeId = Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByName().get(accountType).getRecordTypeId(); 
                    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=recordTypeId;
                    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(); 
                } 
                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{
                    Contact con = new Contact();
                    con.Id=lcr.getContactId();
                    con.FirstName = firstName;
                    //con.AccountId = '001e0000018SOEMAA4';
                    con.LastName = lastName;
                    con.Title = title;
                    con.Email=email;
                    upsert con;
                }
            }
            if(!lcr.isSuccess()){
                status='Lead Conversion Failed';
            }
            finalResult.add(status);            
        }
        catch(Exception ex){
            status = ex.getMessage();
            finalResult.add(status);
            system.debug('-----Exception'+ex.getStackTraceString());
            return finalResult;
        }
        return finalResult;
    }
    
}
Test:-
@isTest(SeeAllData=true)
private class OV_FlwLeadConversionTest {
    @isTest static void testLeadConvert(){
        Lead ld= new Lead();
        ld.LastName = 'test1';
        ld.Company = 'test2';
        ld.FirstName = 'test3';
        ld.Email = 'ssn@gmail.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('test');
        configParam.add('Brokerage');
        configParam.add('Bangalore');
        configParam.add('India');
        configParam.add('test');
        configParam.add('');
        configParam.add('');
        configParam.add(ld.FirstName);
        configParam.add(ld.LastName);
        configParam.add('Master');
        configParam.add(ld.Email);
        configParam.add('Broker');
        configParam.add('1234567890');
        configParam.add('Karnataka');
        configParams.add(configParam);
        
        List<String> finalResult = OV_FlwLeadConversion.convertLead(configParams);
        system.assert(finalResult.size() > 0);   
        
     /**   Database.LeadConvert lc = new Database.LeadConvert();
        lc.setLeadId(ld.Id);
        lc.setDoNotCreateOpportunity(true);
        lc.setConvertedStatus('Qualified');
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
    //    ld = [Select Id , LastName , FirstName , Name , Email , Status from Lead where Id=: ld.Id];
    //    System.assertEquals('Qualified', ld.Status); **/
    }

}
Raj VakatiRaj Vakati
The issue in your code is with the number of params your are setting .whihc cause list index exception . use the below code 
 
@isTest(SeeAllData=true)
private class OV_FlwLeadConversionTest {
    @isTest static void testLeadConvert(){
        Lead ld= new Lead();
        ld.LastName = 'test1';
        ld.Company = 'test2';
        ld.FirstName = 'test3';
        ld.Email = 'ssn@gmail.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('test');
        configParam.add('Brokerage');
        configParam.add('Bangalore');
        configParam.add('India');
        configParam.add('test');
        configParam.add('');
        configParam.add('');
        configParam.add(ld.FirstName);
        configParam.add(ld.LastName);
        configParam.add('Master');
        configParam.add(ld.Email);
        configParam.add('Broker');
        configParam.add('1234567890');
        configParam.add('Karnataka');
             configParam.add('Karnataka');
        configParam.add('Karnataka');
   
        configParams.add(configParam);
        
        List<String> finalResult = OV_FlwLeadConversion.convertLead(configParams);
        system.assert(finalResult.size() > 0);   
        
    }
    
}