• Steven Odorczyk
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

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;
			}	
		}	
	}
}