0

I have a Trigger that is referencing a field (BillingCountry) and validating against a Custom Setting table (ISOCountry__c). The reference is case-sensitive and I would like to make it case-insensitive.

Any help would be appreciated, thanks!

Trigger:

trigger UpdateISOCodeAccount on Account (before insert, before update) {

Map<String, ISOCountry__c> isoCountryMap = ISOCountry__c.getAll();

for(Account a :trigger.new){

    if(isoCountryMap.containsKey(a.BillingCountry))
        a.Billing_Country_Code_2__c = isoCountryMap.get(a.BillingCountry).Country_ISO2_Code__c;
    else a.Billing_Country_Code_2__c= '-';
    if(a.BillingCountry == NULL) {a.Billing_Country_Code_2__c = 'NULL';}
    }
}
  • Just store them lower case and then call toLowerCase before getInstance. – Adrian Larson Dec 8 '16 at 5:06
  • use containsIgnoreCase string method – Shravan Boddula Dec 8 '16 at 5:12
  • @ShravanBoddula there's not getIgnoreCase... – Adrian Larson Dec 8 '16 at 5:52
2

You can store and retrieve the data in lower case if you wish to make the mapping case-insensitive:

public class ISOCodes
{
    public ISOCountry__c get(String isoCode)
    {
        return (isoCode == null) ? null : settings.get(isoCode.toLowerCase());
    }
    static Map<String, ISOCountry__c> settings
    {
        get
        {
            if (settings == null)
            {
                settings = new Map<String, ISOCountry>();
                for (ISOCountry__c setting : ISOCountry__c.getAll().values())
                    settings.put(setting.toLowerCase(), setting);
            }
            return settings;
        }
        private set;
    }
}
0

With a lot of help from Adrian Larson we came to a workable solution with my initial Trigger issue.

I will post the trigger in its current state, however with the knowledge that the trigger now has logic inline (bad practice) which should really be in a TriggerHandler class (something for another day).

Firstly the custom setting has been modified to be in lower case, then...

trigger UpdateISOCodeAccount on Account (before insert, before update) {

for(Account a :trigger.new){

    String bCountry = a.BillingCountry;
    if (bCountry != null) bCountry = bCountry.toLowerCase();

    ISOCountry__c bsetting = ISOCountry__c.getInstance(bCountry);

    if (bsetting != null) a.Billing_Country_Code_2__c = bsetting.Country_ISO2_Code__c;
    else a.Billing_Country_Code_2__c= '-';
    if(bCountry == NULL) {a.Billing_Country_Code_2__c = 'NULL';}

    }
}

Cheers!

  • Just fyi, when an answer addresses your problem, etiquette is generally to accept it by clicking on the check mark/tick to the left of the answer, turning it green. This marks the question as resolved to your satisfaction, and awards reputation both to you and the person who answered. If you have >= 15 reputation points, you may also upvote the answer if you wish. There is no obligation to do either. – Adrian Larson Dec 9 '16 at 15:31

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.