[SalesForce] Invalid convertedStatus

When i am converting lead via apex, i get an error Invalid ConvertedStatus.

  1. I have made sure that the Lead Status has correct converted status.
  2. Also the status is Active
  3. We have recordtypes set on Lead. Also made
    sure that the lead status values are accessible for the
    processes.

The regular lead convert from UI works, somehow its the code which fails

The code is part of managed package, which works on a few orgs, but is failing on one org.

Is there any other setting that i might be missing out which could be causing the error.

          lc.setLeadId(leads.Id);
          lc.setAccountId(opps[0].accountId);
          lc.setConvertedStatus(CStatus); // CStatus has the correct status
          lc.setDoNotCreateOpportunity(donotCreateOpp);
                    if(!donotCreateOpp)
                    {
                        if(leads.Company == null)
                            lc.setOpportunityName(leads.lastname);
                        else
                            lc.setOpportunityName(leads.company);
                    }
          lcs.add(lc);
          lcrs = Database.convertLead(lcs, false);

for(Database.LeadConvertResult lcr : lcrs) {
                System.debug( 'processing a lead convert result');
                if(!lcr.isSuccess()) {
                    System.debug(System.LoggingLevel.ERROR, 'lead conversion failed for lead with id: ' + lcr.getLeadId());
                    //Lead l = newLeads.get(lcr.getLeadId());
                    error = true;

                    for(Database.Error dbe : lcr.getErrors()) {
                        System.debug(System.LoggingLevel.ERROR, dbe.getMessage());
                        //HandleException.LogException(dbe,lcr.getLeadId());
                        Apexpages.addMessage( new ApexPages.Message (ApexPages.Severity.ERROR, 'There was an error converting this Lead. ' + dbe.getmessage()));
                        return null;
                    }
                }
            }

UPDATE :

Looking at documentation here https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_convertlead.htm
Can some help me understand what this means. It refers to lead source on the lead record or on opp?

Record types: 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.

Best Answer

The issue turned out to be that the API name of converted Status was different to MasterLabel. And i was passing the masterlabel on the code which was causing the issue .

Now i changed the api name to be same as the master label and code works fine. Is it better to pass the API name of lead status OR use the Master Label? My gut feeling is to use the API name.

Related Topic