I am getting this error from following batch apex class.

global class SL_Batch_Match_LeadsWithAccounts implements Database.Batchable<sObject>{
    public static set<Id> OppAcctIds;
    //Map<ID, Account> acctMap = new Map<ID, Account>();
    List<Account> acctsList;
    public static List <Opportunity> oppListToUpdateFlag;
    String leadRecordTypeId = Schema.SObjectType.Lead.getRecordTypeInfosByName().get('Sun Homes - LPS').getRecordTypeId();
    global Database.QueryLocator start(Database.BatchableContext BC){
        String query = 'Select AccountId, Flag__c from Opportunity where Flag__c = \'New\'';// And RecordTypeId ='+'\''+leadRecordTypeId+'\'';
        return Database.getQueryLocator(query);
    global void execute(Database.BatchableContext BC, List<sObject>scope){
        //OppAcctIds = new set<Id>();
        //acctsList = new List <Account>();
        oppListToUpdateFlag = new List<Opportunity>();
        List <String> acctAndLeadIdList = new List <String>();
        for(Sobject s : scope){
            Opportunity opp = (Opportunity)s;

            Map<ID, Account> acctMap = new Map<ID, Account>([Select Name, Phone, PersonEmail From Account Where ID =: opp.AccountId]);
            for(ID acctID :acctMap.keySet()){
                if(opp.AccountID == acctID){

                    Map<ID, Lead> leadMap = new Map<ID, Lead>([Select IsConverted from Lead Where Name =:acctMap.get(acctID).Name AND (Phone =:acctMap.get(acctID).Phone OR Email=:acctMap.get(acctID).PersonEmail)]);
                    for(ID leadID :leadMap.keySet()){
                        if(leadID != null && !leadMap.get(leadID).IsConverted){
                            String temp =  acctID + '-' +leadID;



    public void mergLeadWithExistingAccount(List <String> acctAndLeadIdListRecieved){
        list<Database.LeadConvert> leadConverts = new list<Database.LeadConvert>();
        for(String acctAndLeadId: acctAndLeadIdListRecieved){
            String tempAcctId = acctAndLeadId.substringBefore('-');
            String tempLeadId = acctAndLeadId.substringAfter('-');
            Database.LeadConvert lc = new database.LeadConvert();
            //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            //lc.convertedStatus = 'Qualified';
        List <Database.LeadConvertResult> results = Database.convertLead(leadConverts);
        for(Integer i=0; i <= results.size(); i++){
    public void updateOpportuniyFlags(List<Opportunity> oppList){
        List<Opportunity> tempOppList = new List<Opportunity>();

        for (Opportunity opp: oppList){
            opp.Flag__c = 'Processed';
        update tempOppList;
    /*public void matchLeadWithAccount(Lead leadRecievedForMatching){
        for (Account acc: [Select id from Account where Name=:leadRecievedForMatching.Name AND (PersonEmail =:leadRecievedForMatching.Email OR  Phone=:leadRecievedForMatching.Phone)]){
            if (acc.id != Null){
    global void finish(Database.BatchableContext BC){


I also have the converted value checked for this status. Thanks in Advance. Picture is showing that both values are checked

Here is the snip of screen while converting lead manually. enter image description here

  • check converted checkbox is true for this LeadStatus. Oct 26 '16 at 6:13
  • @TusharSharma please read the last line of my question. I have cleared that its checked. Oct 26 '16 at 6:44
  • Message is pretty clear. The status you are adding is not one you can set a converted lead to. You need to query leadstatus for records that are isconvertedstatus and use one of those
    – Eric
    Oct 26 '16 at 6:57
  • @Eric I used the below code LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1]; lc.setConvertedStatus(convertStatus.MasterLabel); and still get following error. First error: ConvertLead failed. First exception on row 0; first error: INVALID_STATUS, invalid convertedStatus: Booked: [Status] Oct 26 '16 at 7:48
  • @TanveerAhmad is that status active ?
    – Eric
    Oct 26 '16 at 14:06

I think this could be due to the Status' not being available for the record types the user has access to / assigned to the lead:

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.


Since everything else checks out this seems to be the only thing left....

  • I have added a snip. Please check it out. Oct 27 '16 at 5:56
  • Could you please review the code? It is not working for more than one record and not updating the opportunities. Oct 27 '16 at 7:49

after another API version update there is a new field representing closing status. instead of MasterLabel you should use ApiName, they may differ.

so the code snippet inside mergLeadWithExistingAccount() method should look like:

LeadStatus convertStatus = [SELECT Id, ApiName FROM LeadStatus WHERE IsConverted=true LIMIT 1];
  • Thank you. This is exactly the case - specially if MasterLabel and ApiName differ. Using MasterLabel to set the status causes the code to fail. To avoid any potential issues, use ApiName. Jan 13 at 4:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.