+ Start a Discussion
jule robinsonjule robinson 

Test Class - Convert Lead

Hello, 

I try to create test class for convert lead class, but i have an error that is . "System.DmlException: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Creation account PA: [Status]".
Can you help me please ?

Class:
Public class AutoConvertLeads
{
    @InvocableMethod
    public static void LeadAssign(List<Lead> LeadtoConvert)
    {
        LeadStatus CLeadStatus= [SELECT Id, MasterLabel, ApiName FROM LeadStatus WHERE IsConverted=true AND ApiName='Creation account PA'];
        List<Database.LeadConvert> MassLeadconvert = new List<Database.LeadConvert>();
        
        for(Lead currentlead: LeadtoConvert){
            	system.debug(currentlead.id);
            	system.debug(currentlead.status);
                Database.LeadConvert Leadconvert = new Database.LeadConvert();
                Leadconvert.setLeadId(currentlead.id);
                Leadconvert.setConvertedStatus(CLeadStatus.ApiName);
                Leadconvert.setDoNotCreateOpportunity(TRUE);
                MassLeadconvert.add(Leadconvert);
        }
        
        if (!MassLeadconvert.isEmpty()) {
            List<Database.LeadConvertResult> lcr = Database.convertLead(MassLeadconvert);
        }
    }
}

Test class:
@isTest(seealldata = true)
public class TestAutoConvertLeads{
    public static testMethod void convertLead(){
        test.startTest();
        Lead leadtoConvert = new Lead();
        leadtoConvert.FirstName='Trigger1';
        leadtoConvert.LastName='Test1';
        leadtoConvert.Company='Trigger Test1';
        leadtoConvert.Exists_In_PartsAdvisor__c=true;
        insert leadtoConvert;
        List<Lead> listToConvert = new List<Lead>();
        listToConvert.add(leadToConvert);
       
        AutoConvertLeads.LeadAssign(listToConvert);
        
        test.stopTest();   
        
    } 
}

Thanks​​​​​​​
Soyab HussainSoyab Hussain
Hi Jule Robinson,

Cause:
The issue is likely that your Salesforce instance does not have the necessary values available for the Soapbox Events Registrations Lead Processes. You can confirm this by going to Setup --> Customize --> Leads --> Lead Processes and clicking on Soapbox Events Registrations. If no values are present in the Selected Values list, Leads will not be auto-converted when they are created.

Solution:
To remedy this, add the following to the Selected Values list and click Save: 
Open - Not Contacted
Working - Contacted
Closed - Converted
Closed - Not Converted

If you found it useful please appreciate my efforts and mark it as the best answer.

LinkedIn:  https://www.linkedin.com/in/soyab-hussain-b380b1194/

Regards,
Soyab