4

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;

            //OppAcctIds.add(opp.AccountId);
            oppListToUpdateFlag.add(opp);
            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;
                            acctAndLeadIdList.add(temp);
                            //mergLeadWithExistingAccount(acctID,leadID);
                        }
                    }
                }
            }
        }

        if(!acctAndLeadIdList.isEmpty()){
            mergLeadWithExistingAccount(acctAndLeadIdList);
        }
        if(!oppListToUpdateFlag.isEmpty()){
            updateOpportuniyFlags(oppListToUpdateFlag);
        }

    }

    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();
            lc.setLeadId(tempLeadId);
            lc.setAccountId(tempAcctId);
            //LeadStatus convertStatus = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1];
            //lc.setConvertedStatus(convertStatus.MasterLabel);
            lc.setConvertedStatus('Qualified');
            //lc.convertedStatus = 'Qualified';
            //Database.ConvertLead(lc,true);
            lc.setDoNotCreateOpportunity(true);
            leadConverts.add(lc);
        }
        List <Database.LeadConvertResult> results = Database.convertLead(leadConverts);
        for(Integer i=0; i <= results.size(); i++){
        System.assert(results[i].isSuccess());
        }
        System.debug(results);
    }
    public void updateOpportuniyFlags(List<Opportunity> oppList){
        List<Opportunity> tempOppList = new List<Opportunity>();

        for (Opportunity opp: oppList){
            opp.Flag__c = 'Processed';
            tempOppList.add(opp);
        }
        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){
                //mergLeadWithExistingAccount(acc.id,leadRecievedForMatching);
            }
        }
    }*/
    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

9
  • 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
2

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.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_dml_examples_convertlead.htm

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

2
  • 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
4

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];
lc.setConvertedStatus(convertStatus.ApiName);
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.