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
BigSmokeBigSmoke 

Database.ConvertLead() not working INVALID_STATUS

Hi,

 

I'm trying to write a class that will autoconvert certain leads to existing opportunities without creating an opportunity, given a lead, a contact Id, an account Id, and an owner Id.

 

I'm using the following code to troubleshoot my problem:

public class autoConvertLead {

	// use this lead as a test
    public PageReference cL() {
		Id id = '00QM0000001LJrl';
		set<Id> idSet = new set<Id>{id};
		
        convertList(idSet);
        return null;
    }


    public void convertList(set<Id> leads) {

LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus 
 where MasterLabel like '%auto%match%' and IsConverted=true limit 1];

        Lead[] leadList = [select Id, account__c, contact__c, contact__r.AccountId,  
        account__r.ownerId from Lead 
                where Id in :leads and isConverted = false];
                    
        for (Lead lead : leadList) {

        system.assert(lead.account__c != null);
        system.assert(lead.contact__c != null);
        system.assert(lead.account__c == lead.contact__r.AccountId);   
                                     
        Database.LeadConvert lc = new database.LeadConvert();
            lc.setLeadId(lead.id);
            lc.setDoNotCreateOpportunity(true); 
          //  lc.setOwnerId(lead.account__r.ownerId);
            lc.setAccountId(lead.account__c);
            lc.setContactId(lead.contact__c);
            lc.setOverwriteLeadSource(false);
			lc.setSendNotificationEmail(false);
    		lc.setConvertedStatus(convertStatus.MasterLabel);

        system.assert(convertStatus.MasterLabel != null);
        system.debug(lc);
                
        Database.LeadConvertResult lcr = Database.convertLead(lc);
        System.assert(lcr.isSuccess());
        }
    }
}

 

 

I'm getting "INVALID_STATUS, invalid convertedStatus: Convert - Auto-match to Existing Contact: []: Class.autoConvertLead.convertList: line 36, column 58".

 

I'm taking all the precautions in my code.  I'm ensuring the status value is valid first before using it.:

LeadStatus convertStatus = [Select Id, MasterLabel from LeadStatus 
 where MasterLabel like '%auto%match%' and IsConverted=true limit 1];

 

I'm ensuring the lead has the requisite IDs for everything else.  

 

I'm able to convert a clone of the same lead (same record type too)  through the usual steps in the Salesforce.com UI and converting the lead that way, selecting exactly the same Status and I'm having no problem with that.

 

There isn't very much in the way of postings around this topic.

 

Are there any lead conversion wizards out there??

 

Thanks.

Best Answer chosen by Admin (Salesforce Developers) 
BigSmokeBigSmoke

And now the kicker:  All these errors and strange behaviors were all down to one thing:  

 

The sandbox's data allocation was up to 97%.

 

Delete a bunch of data and pow ... like magic.

 

Now how's that for a day and a half wasted on totally misleading error messages??

All Answers

BigSmokeBigSmoke

I deleted all Lead Recordtypes from the system and now this works.  Why wouldn't it work when there are recordtypes on Leads, and then work with no alterations where there aren't any?

BigSmokeBigSmoke

And now the kicker:  All these errors and strange behaviors were all down to one thing:  

 

The sandbox's data allocation was up to 97%.

 

Delete a bunch of data and pow ... like magic.

 

Now how's that for a day and a half wasted on totally misleading error messages??

This was selected as the best answer
Baird_SBaird_S
For me, the issue was that one Lead RecordType did not have a Lead Process.  After I gave it a process, the conversion ran fine.  Ouch.