0

I'm getting these Errors NullPointerException: Attempt to de-reference a null object. and the line are: Class.LeadConvertUtil.convertLead: line 30, column 1 enter image description here Class.Test_LeadConvertUtil.leadConvertUtilTest: line 58, column 1 enter image description here how to fix it?

the Class code is:

    public class LeadConvertUtil { 

  public static void convertLead(Lead leadRcd){

    String leadRecordTypeName = leadRcd.Lead_Record_Type_Name__c;
    System.debug('Lead Record Type :'+leadRecordTypeName);

    String convertLeadId = leadRcd.Id;

    List<RecordType> accountRecordTypes = [select Id, Name 
                                           from RecordType 
                                           where SobjectType = 'Account' 
                                           and Name=:leadRecordTypeName];    
    List<RecordType> contactRecordTypes = [select Id, Name 
                                           from RecordType 
                                           where SobjectType = 'Contact' and Name=:leadRecordTypeName];

    Database.LeadConvert lc = new database.LeadConvert();
    lc.setLeadId(convertLeadId);
    lc.setDoNotCreateOpportunity(true);
     LeadStatus convertStatus;
    try{
        convertStatus = [SELECT Id, MasterLabel 
                                FROM LeadStatus 
                                WHERE IsConverted=true and MasterLabel='Converted' and Id != null]; 
    }catch(System.QueryException e){
        System.debug('caught ya!');
    }

    lc.setConvertedStatus(convertStatus.MasterLabel);     

    try{

        System.debug('Converting Lead with id '+convertLeadId);
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        String accntId = lcr.getAccountId();
        String cntId = lcr.getContactId();
        System.debug('getAccountId: '+ accntId);
        System.debug('getContactId: '+ cntId);
        System.debug('getLeadId: '+ lcr.getLeadId());
        System.debug('isSuccess: '+ lcr.isSuccess()); 

        //Account Updates
        Account convertedAccount;
        try{
            convertedAccount = [Select id from Account where id=:accntId];
            }catch(Exception e){
                convertedAccount =null;
            }


        if(accountRecordTypes!=null && accountRecordTypes.size()>0){            
            convertedAccount.recordTypeId = accountRecordTypes.get(0).Id;           
        }
        convertedAccount.Converted_Lead_ID__c = convertLeadId;        
        update convertedAccount;

        //Contact Updates
        if(contactRecordTypes!=null && contactRecordTypes.size()>0){
            Contact convertedContact = [Select id from Contact where id=:cntId];
            convertedContact.recordTypeId = contactRecordTypes.get(0).Id;    
            update convertedContact;        
        }

        } catch(DmlException e) {
            System.debug('Exception: '+ e);
        }
    } 
}

the Test class code is:

    /**
 * This class contains unit tests for validating the behavior of LeadConvertUtil
 *
 */
@isTest
public class Test_LeadConvertUtil{ 

    static testMethod void leadConvertTrigrTest() {
        //test.startTest();
        //Lead VPS Record Type
        // BEGIN:  S-444340, T-567488 - include Lead record type = 'IAA VPS Sales'
        List<RecordType> leadRecordTypes = [select Id, Name 
                                            from RecordType 
                                            where SobjectType = 'Lead' and Name='IAA VPS Sales']; 
        //Insert Test Lead
        Lead ldTst = new Lead();
        ldTst.FirstName = 'Lead1';
        ldTst.LastName = 'Lead';
        ldTst.email = '[email protected]';
        ldTst.Company = 'TestCorp';
        ldTst.Contract_Type__c = 'CFF';

        if(leadRecordTypes!=null && leadRecordTypes.size()>0){          
            ldTst.recordTypeId = leadRecordTypes.get(0).Id;         
        }

        ldTst.Lead_Stage__c = 'Account Activated';
        ldTst.Status ='Converted';

        insert ldTst;
    }   


    static testMethod void leadConvertUtilTest() {

        //Lead VPS Record Type
        // BEGIN:  S-444340, T-567488 - include Lead record type = 'IAA VPS Sales'
        List<RecordType> leadRecordTypes = [select Id, Name 
                                            from RecordType 
                                            where SobjectType = 'Lead' and Name='IAA VPS Sales'];

        //Insert Test Lead
        Lead ldTst = new Lead();
        ldTst.FirstName = 'Lead1';
        ldTst.LastName = 'Lead';
        ldTst.email = '[email protected]';
        ldTst.Company = 'TestCorp';
        ldTst.Contract_Type__c = 'CFF';
        //ldTst.RecordType.Name = 'IAA VPS Sales';

        if(leadRecordTypes!=null && leadRecordTypes.size()>0){          
            ldTst.recordTypeId = leadRecordTypes.get(0).Id;         
        }
        ldTst.Lead_Stage__c = 'Account Activated';
        ldTst.Status ='Converted';
        insert ldTst;

        LeadConvertUtil.convertLead(ldTst);

       //test.stopTest(); 

    }   
}

1 Answer 1

1

convertStatus is null. My guess is that you have more than one valid converted status. You didn't get the appropriate error because you used a try-catch block to catch the QueryException without actually reporting an error. Consider changing your code from this:

try{
    convertStatus = [SELECT Id, MasterLabel 
                            FROM LeadStatus 
                            WHERE IsConverted=true and MasterLabel='Converted' and Id != null]; 
}catch(System.QueryException e){
    System.debug('caught ya!');
}

To this:

convertStatus = [SELECT Id, MasterLabel 
                            FROM LeadStatus 
                            WHERE IsConverted=true and MasterLabel='Converted' LIMIT 1]; 

In general, if you're not prepared to handle an exception, do not use a try-catch block. See this question and answer for when using try-catch is okay, and when it's not.

2
  • if i don't have Try catch i get error on the soql query: List has not rows for assignment to SObject
    – Templar
    Commented Mar 3, 2017 at 22:52
  • @Templar So that's your real problem: you have the wrong status value that you're trying to query. Make sure the label is what you expect it to be.
    – sfdcfox
    Commented Mar 3, 2017 at 23:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .