+ Start a Discussion
Mark G. SchaferMark G. Schafer 

UNKNOWN_EXCEPTION on attempt to convert lead using Apex

I am writing some code that will automatically convert leads in certain situations. Additionally, the code searches for contacts with the same email address, and associates the converted lead with the existing Contact and the Contact's related Account. If there is no matching Contact, it searches for an Account where a custom field called Domain__c matches the value of Domain__c on the Lead being converted. If that's found, the Lead is converted to a new Contact on an existing Account. Otherwise, it creates a new Contact and Account.

Here is the error I am receiving. So far, Salesforce support has not been helpful in pointing me in the right direction.

Error:Apex trigger leadTrigger caused an unexpected exception, contact your administrator: leadTrigger: execution of AfterUpdate caused by: System.DmlException: ConvertLead failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, An unexpected error occurred. Please include this ErrorId if you contact support: 711698393-71813 (759071441): []: Class.LeadConverter.convertLead: line 143, column 1

I wrote this as a static method for now, but I'll be updating to make the code more generic to handle multiple different cases once I get this version working. Here's my trigger:

trigger leadTrigger on Lead (before insert, before update, after insert, after update) {
    //before insert 
    if(Trigger.isBefore && Trigger.isInsert){
        
    } 
    //before update 
    if(Trigger.isBefore && Trigger.isUpdate){
        
    }
    //after insert
    if(Trigger.isAfter && Trigger.isInsert){
        LeadConverter.convertLead(Trigger.new);
    }
    //after update
    if(Trigger.isAfter && Trigger.isUpdate){ 
        LeadConverter.convertLead(Trigger.new);
    }
}


And here is my Class:

public class LeadConverter {

	public static void convertLead(List<Lead> leads){
		Set<String> domains = new Set<String>();
		Set<String> emailAddresses = new Set<String>();
		List<Lead> leadsToConvert = new List<Lead>();
		List<Database.LeadConvert> leadConverts = new List<Database.LeadConvert>();
		
		LeadStatus convertStatus = [SELECT MasterLabel FROM LeadStatus WHERE IsConverted = true LIMIT 1];
		
		for (Lead l: leads){
			if(l.LeadSource == 'SSL Distro' && l.Domain__c != null
			 && l.Email != null && !l.isConverted){
				if(l.Domain__c != null){ domains.add(l.Domain__C); }
				if(l.Email != null){ emailAddresses.add(l.Email); }
				leadsToConvert.add(l);
			}//end if statement on Lead Source	
		}//end leads for loop
        
        List<Account> exstingAccounts = [SELECT Id, Name, Domain__c FROM Account WHERE Domain__c IN :domains];
        List<Contact> exstingContacts = [SELECT Id, AccountID, FirstName, LastName, Email, Email_ID__c 
        								FROM Contact WHERE Email_ID__c IN : emailAddresses];
        								
        Map<String, Contact> emailContactIdMap = new Map<String, Contact>();	
        Map<String, Account> domainAccountIdMap = new Map<String, Account>();	
        	
        for (Contact c: exstingContacts){ emailContactIdMap.put(c.Email_ID__c, c); }						
		for (Account a: exstingAccounts){ domainAccountIdMap.put(a.Domain__c, a); }
		
		for (Lead l: leadsToConvert){
			Database.LeadConvert lc = new Database.LeadConvert();
			if (emailContactIdMap.get(l.Email) != null && !l.isConverted){
				if(emailContactIdMap.get(l.Email).AccountId != null){
					lc.setContactId(emailContactIdMap.get(l.Email).Id);
					lc.setAccountId(emailContactIdMap.get(l.Email).AccountId);
				}	
			} else if (domainAccountIdMap.get(l.Domain__c) != null && !l.isConverted){
				lc.setAccountId(domainAccountIdMap.get(l.Domain__c).Id);
			}
			lc.setLeadId(l.Id);
			lc.setDoNotCreateOpportunity(true);
			lc.setConvertedStatus(convertStatus.MasterLabel); 
			
			leadConverts.add(lc);
		}//end leads to convert for loop
		
		System.debug('The list of items to convert has a size of: ' + leadConverts.size() );
		
			if(!leadConverts.isEmpty()){
				List<Database.LeadConvertResult> lcr = Database.convertLead(leadConverts);
			}
		
	}
}

Does anyone have any insight? I've never encountered such a generic exception before, and not even sure where to start with this.
Mark G. SchaferMark G. Schafer
And one note... I omitted a bunch of lines of commented-out code in the example above. Line 143 references in the error above actually lines up with line 50 in the code snippet I include in this post, so the exception occurs on the line where I attempt to actually do the conversions.
Mark G. SchaferMark G. Schafer
I've also tried commenting out the lines of code related to finding existing contacts and accounts, and simply convert the lead, and the error persists.
William LópezWilliam López
Mark seems strange.

I tested your code in a fresh demo org and it works ok.

So I think the issue might be in another place. Like a account validation rule, its not the trigger that its failing it has to be something after trigger completes the Contact and Account creation that fails and this make the convertion to fail.

Do you have the full debug log of the exeption ? 
Leslie  KismartoniLeslie Kismartoni
I copied your code into a new DEV org, added the custom fields and... It worked.

Often times an Unknown Exception bubbles up when there is some type information/mapping that is incorrect - but I don't see you mapping anything goofy in these cases. 

If you copy this code and recreate the Domain__c and Email_Id__c fields on the respective objects, does it still break?

Is there any other configuration/setting that you might not know of that causes Lead Conversion to break? (Currency settings?)

last thing you might be able to do is look at the heap. Check this article out: https://nefdev.wordpress.com/2013/04/24/salesforce-generates-unknown-exception-during-deployment/
Mark G. SchaferMark G. Schafer

Thanks William and Leslie. I dug into the debug log again and found that the exception was occuring when an installed package for Zendesk was doing something (which I had previously missed). I actually uninstalled the package and then reinstalled it and it seems to be working now.

I haven't been able to track down precisely what was causing the issue, especially because I can't see everything the code in the installed package is doing, but at least I know the culprit now.

I appreciate the help looking into this.