2

Before implementing country and state picklists we want to change our APEX to insert country names instead of country codes.

Now. I could just have a big map of country codes to country names but the names might not match Salesforce's DB (for example "United States of America" versus "United States") and this is also very fragile.

Is there a better way?

2

Those ISO codes are standard, so if you stick to the ISO standards you should be okay. Are you trying to maintain a combination before you switch over? If that's the case why then you could put the combination in custom settings, the key being the two letter country code, the value being country name. Sure you will still have a "big map" but it will be maintained through custom settings and you won't have to update code to change it.

But if you are that concerned about them changing (which I don't think they do), you could hit an API (Google around) that you can hit to update them. I don't believe there is enough risk here though for you to do that

Create a custom setting

private static Map<String, ISOCodes__c> isoCodes = ISOCodes__c.getAll();

for(ResponseObject ro : ResponseObject)
{
    if(isoCodes.containsKey(ro.twoDigitISO)
    {
        yourUpdateObject.CountryName__c = isoCodes.get(ro.twoDigitISO);
    }   
}

This is rough but, this is all youd need to do

| improve this answer | |
  • We have a JSON API endpoint which takes in two letter country codes as a parameter. I'd rather not change the interfaces because the country codes are standard but for storing things in the database I want everything to be the same Salesforce standard country name format and not a mix of country codes and country names. – Steven Stewart-Gallus Jun 5 '17 at 18:42
  • I think I understand what your saying, you want to use the country pick lists and if they select United States you want it to populate your other field to US, then you would need to do that through custom settings to maintain that mapping, or you could create a Lookup and then you could utilize formula fields, I'd only recommend that if you need to store details about your countries and iso codes. Salesforce uses ISO-3166, so there is little to now risk of differences – EricSSH Jun 5 '17 at 18:52
  • No. You completely misunderstand me. I have no idea how you got there. I have a PHP script that sends a two letter country code to some APEX. The APEX needs to change that country code to a country name before storing it in the database. – Steven Stewart-Gallus Jun 5 '17 at 19:35
  • So you need a custom setting, store the combination of the 2 letter country code, and the country. The custom setting Name will be US (2 letter code), and create another field called CountryName__c which will be United States Of America. When you consume the service you can then update your db, so essentially my answer above – EricSSH Jun 5 '17 at 19:39

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.