• Mark G. Schafer
  • NEWBIE
  • 10 Points
  • Member since 2015
  • Yesware

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 7
    Replies

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.

I recently switched on the state and country picklist feature in my org, but I'm running into a problem with many of the leads we get routed into our org from my company's AppExchange listing. We're getting emails with the subject 'Salesforce Could Not Create This Lead' because the leads contain a country name (most commonly 'US') that doesn't map to the country value in the standard Country picklist.

I've attempted to write some Apex that will take the text value from the incoming lead and attempt to correct the non-standard country value using a list of countries I store in a custom object. In the debug log, the code I wrote below seems to succesfully find the standard country name and code and write it to the CountryName and CountryCode fields on the Lead.

Even so, I still receive an error when I try to insert a test Lead record with the value of 'US' in the Country field via the DataLoader, even the the debug log confirms I've updated the Country field to 'United States' and the CountryCode field to 'US'. The returned error is (There's a problem with this country, even though it may appear correct. Please select a country from the list of valid countries.: Country).

This seems weird, because I think Salesforce does system validations after before triggers are run.

I've tried editing the code to only update the Country field, only update the CountryCode field, and to update both (as written below). They all end up throwing the same error.

Anyone have any ideas?

public class StateCountryGeoLookup {

	public static void validateLeadCountry (List<Lead> leadList){
		
		//query of a custom object containing all country names, codes, and alternate spellings
		List <Country_Lookup__c> Countries = [SELECT Name, ID, ISO_2_Code__c, ISO_3_Code__c, 
											Alternate_Name_1__c, Alternate_Name_2__c
											FROM Country_Lookup__c];
											
		Map<String,String> countryMap = new Map<String,String>();
		Map<String,String> countryCodeMap = new Map<String,String>();
		
		//map every variant of the country name to the standard country name and code in the picklist
		for (Country_Lookup__c country : Countries){
			countryMap.put(country.Name, country.Name);
			countryCodeMap.put(country.Name, country.ISO_2_Code__c);
			
			countryMap.put(country.ISO_2_Code__c, country.Name);
			countryCodeMap.put(country.ISO_2_Code__c, country.ISO_2_Code__c);
			
			countryMap.put(country.ISO_3_Code__c, country.Name);
			countryCodeMap.put(country.ISO_3_Code__c, country.ISO_2_Code__c);
			
			if(country.Alternate_Name_1__c != null){
				countryMap.put(country.Alternate_Name_1__c, country.Name);
			}
			if(country.Alternate_Name_2__c != null){
				countryMap.put(country.Alternate_Name_2__c, country.Name);
			}	
		}
		
		for (Lead l : leadList){
			String countryName = countryMap.get(l.Country);
			String countryCode = countryCodeMap.get(l.Country);
			
			System.debug('The returned country name is ' + countryName);
			System.debug('The returned country code is ' + countryCode);
			
			if (countryName != null && countryCode !=null){
				
				//update Lead fields with valid country name and code
				l.Country = countryName;
				l.CountryCode = countryCode;
				
				System.debug('The lead field Country has a value of ' + l.Country);
				System.debug('The lead field CountryCode has a value of ' + l.CountryCode);
				
			//remove the value entirely if a valid country name can't be found	
			} else {
				l.Country = null;
				l.CountryCode = null;
			}	
		}	
	}
}
 



 

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.

I recently switched on the state and country picklist feature in my org, but I'm running into a problem with many of the leads we get routed into our org from my company's AppExchange listing. We're getting emails with the subject 'Salesforce Could Not Create This Lead' because the leads contain a country name (most commonly 'US') that doesn't map to the country value in the standard Country picklist.

I've attempted to write some Apex that will take the text value from the incoming lead and attempt to correct the non-standard country value using a list of countries I store in a custom object. In the debug log, the code I wrote below seems to succesfully find the standard country name and code and write it to the CountryName and CountryCode fields on the Lead.

Even so, I still receive an error when I try to insert a test Lead record with the value of 'US' in the Country field via the DataLoader, even the the debug log confirms I've updated the Country field to 'United States' and the CountryCode field to 'US'. The returned error is (There's a problem with this country, even though it may appear correct. Please select a country from the list of valid countries.: Country).

This seems weird, because I think Salesforce does system validations after before triggers are run.

I've tried editing the code to only update the Country field, only update the CountryCode field, and to update both (as written below). They all end up throwing the same error.

Anyone have any ideas?

public class StateCountryGeoLookup {

	public static void validateLeadCountry (List<Lead> leadList){
		
		//query of a custom object containing all country names, codes, and alternate spellings
		List <Country_Lookup__c> Countries = [SELECT Name, ID, ISO_2_Code__c, ISO_3_Code__c, 
											Alternate_Name_1__c, Alternate_Name_2__c
											FROM Country_Lookup__c];
											
		Map<String,String> countryMap = new Map<String,String>();
		Map<String,String> countryCodeMap = new Map<String,String>();
		
		//map every variant of the country name to the standard country name and code in the picklist
		for (Country_Lookup__c country : Countries){
			countryMap.put(country.Name, country.Name);
			countryCodeMap.put(country.Name, country.ISO_2_Code__c);
			
			countryMap.put(country.ISO_2_Code__c, country.Name);
			countryCodeMap.put(country.ISO_2_Code__c, country.ISO_2_Code__c);
			
			countryMap.put(country.ISO_3_Code__c, country.Name);
			countryCodeMap.put(country.ISO_3_Code__c, country.ISO_2_Code__c);
			
			if(country.Alternate_Name_1__c != null){
				countryMap.put(country.Alternate_Name_1__c, country.Name);
			}
			if(country.Alternate_Name_2__c != null){
				countryMap.put(country.Alternate_Name_2__c, country.Name);
			}	
		}
		
		for (Lead l : leadList){
			String countryName = countryMap.get(l.Country);
			String countryCode = countryCodeMap.get(l.Country);
			
			System.debug('The returned country name is ' + countryName);
			System.debug('The returned country code is ' + countryCode);
			
			if (countryName != null && countryCode !=null){
				
				//update Lead fields with valid country name and code
				l.Country = countryName;
				l.CountryCode = countryCode;
				
				System.debug('The lead field Country has a value of ' + l.Country);
				System.debug('The lead field CountryCode has a value of ' + l.CountryCode);
				
			//remove the value entirely if a valid country name can't be found	
			} else {
				l.Country = null;
				l.CountryCode = null;
			}	
		}	
	}
}