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
EmilyMEmilyM 

How to get the appropriate converted LeadStatus based on Lead RecordType

I'm getting the following error when attempting to convert a Lead:

System.DmlException: ConvertLead failed. First exception on row 0; first error:
INVALID_STATUS, invalid convertedStatus: Closed - Converted: []

I have determined that this is happening because I have more than one converted LeadStatus.  Different Lead RecordTypes have corresponding Lead Processes associated to them, each one using a different converted LeadStatus.

The code I have to pick LeadStatus is:

LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus  where IsConverted=true limit 1];

Obviously, this just picks the first available converted LeadStatus, which for some RecordTypes is not the appropriate one.

Could someone please point me to a code sample that does a smarter
LeadStatus selection?

 

Also, I'd like to understand why convertLead started failing with this error in the last month or so, since everything else stayed the same.  Someone suggested in the forum that there were several issues after the Spring 12 release.
Thanks!
Best Answer chosen by Admin (Salesforce Developers) 
Marty Y. ChangMarty Y. Chang

If you have defined record types for Leads in your org, you'll be able to access the Lead.RecordTypeId field.  To reliably translate that ID into the name of a record type, I would add the following property and method to the LeadUtil class:

 

private static final Map<String, Schema.RecordTypeInfo> recordTypeInfosByID =
            Schema.SObjectType.Contract.getRecordTypeInfosByID();

public static String getRecordTypeName(Id id) {
	return recordTypeInfosByID.get(id).getName();
}	// public static String getRecordTypeName(Id)

 

I would check out the "sObject Describe Result Methods" article in the Apex Developer's Guide for more info.

All Answers

Marty Y. ChangMarty Y. Chang

Hello, EmilyM,

 

It doesn't look like there's a good way to figure out the appropriate converted Lead Status yet, at least in Spring '12.  There doesn't seem to be an SObject that connects LeadStatus to RecordType, and I don't see any useful methods for RecordTypeInfo that can be obtained from a describe result.

 

If you're committed to using Apex, the next best thing may be to create a utility class that contain a pseudo-constant Map of Record Types to converted Lead Status values.

 

public class LeadUtil {
    
    /**
     * Record Type for donation leads.
     */
    public static final String DONATION_LEAD_RECORD_TYPE_NAME =
            'Donation Lead';
    
    /**
     * Record Type for enrollment leads.
     */
    public static final String ENROLLMENT_LEAD_RECORD_TYPE_NAME =
            'Enrollment Lead';
    
    /**
     * Record Type for enrollment leads.
     */
    public static final String SUBSCRIPTION_LEAD_RECORD_TYPE_NAME =
            'Subscription Lead';
    
    /**
     * Map of default converted Lead Status values for each record type,
     * keyed by the name of the record type.
     */
    public static Map<String, String> convertedLeadStatusesByRecordTypeName =
            new Map<String, String> {
                DONATION_LEAD_RECORD_TYPE_NAME =>
                        'Closed - Converted into Donation',
                ENROLLMENT_LEAD_RECORD_TYPE_NAME =>
                        'Closed - Converted into Enrollment',
                SUBSCRIPTION_LEAD_RECORD_TYPE_NAME =>
                        'Closed - Converted into Subscription'
            };
    
}   // public class LeadUtil

 

EmilyMEmilyM

Thanks, Marty!  That is helpful.

 

One more question:  how do I get the record type from a lead?

 

Thanks!

Marty Y. ChangMarty Y. Chang

If you have defined record types for Leads in your org, you'll be able to access the Lead.RecordTypeId field.  To reliably translate that ID into the name of a record type, I would add the following property and method to the LeadUtil class:

 

private static final Map<String, Schema.RecordTypeInfo> recordTypeInfosByID =
            Schema.SObjectType.Contract.getRecordTypeInfosByID();

public static String getRecordTypeName(Id id) {
	return recordTypeInfosByID.get(id).getName();
}	// public static String getRecordTypeName(Id)

 

I would check out the "sObject Describe Result Methods" article in the Apex Developer's Guide for more info.

This was selected as the best answer
EmilyMEmilyM

Thank you!

Mary Beth LeMay 3Mary Beth LeMay 3
HI Marty - I'm in the same boat and not successful.  I really am not so much of a coder (admin) but trying my best - I think I set up the LeadUtil class correctly tailored to my situation, but it's not clear to me if I need to add this code to and how/where I add it in the class or if it's something different.  

private static final Map<String, Schema.RecordTypeInfo> recordTypeInfosByID = Schema.SObjectType.Contract.getRecordTypeInfosByID(); public static String getRecordTypeName(Id id) { return recordTypeInfosByID.get(id).getName(); } // public static String getRecordTypeName(Id)

here is the modified lead until class code I did - any help you can provide is greatly appreciated.

public class LeadUtil {
            // public static String getRecordTypeName(Id)
    /**
     * Record Type for donation leads.
     */
    public static final String  GENERAL_LEAD_RECORD_TYPE_NAME =
            'Lead - General';
    
    /**
     * Record Type for Mentor leads.
     */
    public static final String MENTOR_LEAD_RECORD_TYPE_NAME =
            'Mentor - Lead';
   
    /**
     * Map of default converted Lead Status values for each record type,
     * keyed by the name of the record type. */
    public static Map<String, String> convertedLeadStatusesByRecordTypeName =
            new Map<String, String> {
                GENERAL_LEAD_RECORD_TYPE_NAME =>
                        'Closed - Converted',
                MENTOR_LEAD_RECORD_TYPE_NAME =>
                        'Matched'
                    };
}