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
sunny@99-chgsunny@99-chg 

Error: ConvertLead Failed

Hai All,

 

When i am converting a lead Its giving Error

Error: ConvertLead Failed: First exception on row 0; First error: UNAVAILABLE_RECORDTYPE_EXCEPTION,unable to find defaultnrecord type[]

 

Below is the Class code On convert lead

// This class is meant to hold all the code which will be reusable and common processes. Some webservice methods we can write here also.

global class Lead_Manager
{
    // Responsible for lead conversion from standard page layout.
    // Below webservice method is being called on the click event of the custom convert button on the standard lead page layout.
    WebService static String convertLeadByConvertCustomeButton(String leadId)
    {
        try
        {
            Lead led = [Select id,Name,Status,LeadSource ,IsConverted ,(Select id,ContentType,Name,LastModifiedDate from Attachments) from Lead where id = :leadId];
            if(led.Attachments.size()==0)
                return 'Please upload documents before converting the lead.';
            if(led.Status=='Approved')
            {

             // Here it is throwing Error
                Database.LeadConvert lc = new database.LeadConvert();
                lc.setLeadId(leadId);
                LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
                lc.setConvertedStatus(convertStatus.MasterLabel);
                lc.setDoNotCreateOpportunity(true);
                Database.LeadConvertResult lcr = Database.convertLead(lc);
                //ApexPages.addMessage(new ApexPages.message(ApexPages.severity.confirm,'Lead has been converted successfully.'));
                led = [Select id,Name,Status,LeadSource ,IsConverted ,ConvertedAccountId,ConvertedContactId from Lead where id = :leadId];
                /* List<Contact> contactList = [Select id from Contact where id=:led.ConvertedContactId];  
                if(contactList.size()>0)
                    delete contactList; */
                return String.ValueOf(led.ConvertedAccountId);
            }
            else
                return 'CSR has not approved the lead so it can not be converted.';
            
        }
        catch(System.Exception e)
        {
            return e.getMessage();
        }
    }
 
    //Lead conversion custom method. This method will take an instance of Lead object
    //and will convert it without creating opportunities and returned the converted lead to its caller.
    // Responsible for lead conversion from visual force.
    public static Lead convertLead(Lead lead)
    {
        Database.LeadConvert lc = new database.LeadConvert();
        lc.setLeadId(lead.Id);
        LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
        lc.setConvertedStatus(convertStatus.MasterLabel);
        lc.setDoNotCreateOpportunity(true);
        
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        Lead led = [Select id,Status,LeadSource ,ConvertedAccountId,RecordType.Name,Name,OwnerId,CreatedById,FirstName,LastName,Street,City,State,PostalCode,Email,phone,Last_4_digits_SSN__c,Date_of_Birth__c,IsConverted,
                            Enrollment_Id__c,EBT_Number__c,EBT_Holder__c,Number_of_Persons_or_Household__c,Monthly_Income__c,Yearly_Income__c
                            from Lead
                            where id = :lead.id];
       return led;
    }
 
 
    @RemoteAction
    global static String checkLeadApprovalStatusByCSR(String leadId)
    {
        Lead lead = [Select id,Status from Lead where id = :leadId];
        if(lead.Status == 'Approved')
            return '1';
        else
            return '0';
    }
 
}

Afzal MohammadAfzal Mohammad

Make sure you've got some default recordtypes assigned to owner's profile.

 

Here is an extract from apex dev guide:

 

If the organization uses record types, the default record type of the new owner is assigned to records created during lead conversion. The default record type of the user converting the lead determines the lead source values available during conversion. If the desired lead source values are not available, add the values to the default record type of the user converting the lead. For more information about record types, see the Salesforce online help.

 


Hope that helps.

 

Afzal,

chechecheche

hi sunny@99, salesforce newbie here.

 

have you figured out why the error is showing up? im encountering the same error.  thanks!

KalyanGKalyanG
Hi @sunny@99-chg @cheche , I'm also getting this error while doing a custom conv process. I know that my users don't have a default record type, but I'm adding a permission set to add that record type. Still getting this error.