Lead conversion in test class error – Invalid Converted Status

failing-testslead-conversionunit-test

I have a test class that's doing a Lead conversion that's resulting in an "Invalid Converted Status" error.

This is the part of the test class that's converting the lead:

Test.startTest();
    // Convert Lead to Account, Contact and Opportunity
    Database.LeadConvert lc = new database.LeadConvert();  
    lc.setLeadId( objLead.Id );
    lc.setDoNotCreateOpportunity(false);
    lc.setConvertedStatus( 'SQL' );  

    
    // Get result of Lead conversion
    Database.LeadConvertResult lcr = Database.convertLead(lc, false);  

Test.stopTest();

On the Lead object, 'SQL' is listed as a converted status:

Lead Status Picklist Values

I've tried the other picklist values that are listed as converted, but get the same error. I've confirmed that there is no hidden space, and I'm able to use this converted value from the UI with no issue.

What could be causing this error?

Full test class code below for reference:

@isTest
public with sharing class LeadConversionNotificationCalloutTest {
@isTest
static void testLeadConv() {  
    // Create test Lead record
    Lead objLead = new Lead( FirstName = 'Test', 
                            LastName = 'Sample', 
                            Company = 'Testing Sample Co' , 
                            Email = '[email protected]', 
                            Status = 'Prospect', 
                            What_made_you_interested_in_Doorstead__c = 'Too busy to self manage');  
    insert objLead;  
    
    Property__c objProperty = new Property__c(Name = 'Test Property',
                                             Property_Type__c = 'single-family property',
                                             Lifecycle_Stage__c = 'Acquisition');

    Offer__c objOffer = new Offer__c(Name = 'Test Offer', 
                                     Offer_Id__c = 'skjwicn@34SD', 
                                     Lead__c = objLead.Id);
    insert objOffer;

    objLead.Current_Offer__c = objOffer.Id;
    objLead.Property__c = objProperty.Id;
    update objLead;

    // Set mock callout class 
    Test.setMock(HttpCalloutMock.class, new LeadConversionNotificationCalloutMock());
    
    Test.startTest();
    // Convert Lead to Account, Contact and Opportunity
    Database.LeadConvert lc = new database.LeadConvert();  
    lc.setLeadId( objLead.Id );
    lc.setDoNotCreateOpportunity(false);
    lc.setConvertedStatus( 'SQL' );  

    
    // Get result of Lead conversion
    Database.LeadConvertResult lcr = Database.convertLead(lc, false);  
    Test.stopTest();


    
    // Post any errors to the debug log
    system.debug( 'Errors are ' + lcr.getErrors() );  
      
    system.assert( lcr.isSuccess() );  
      
}
}

Best Answer

Did you try obtaining the status from apex? It looks something like this

LeadStatus convertedStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
Database.LeadConvert convert = new Database.LeadConvert();
convert.setDoNotCreateOpportunity(True);
convert.setLeadId(objLead.Id); 
convert.setConvertedStatus(convertedStatus.MasterLabel);
Database.LeadConvertResult lcResult = Database.convertLead(convert, false);

You should be able to navigate through your converted statuses and choose the right value for the lead that you are using for testing.

Remember also you should match RecordType - LeadProcess - converted status for that rercordtype - leadprocess. It needs to be a visible converted status for the lead you are using