0

Currently we have a trigger on Account object which updates one field based on certain conditions and if that field value matches with another object where all the values are mentioned. One of the conditions we are also checking on running user's country field like this

if (usrcountry == 'Australia' || usrcountry == 'New Zealand'  || usrcountry == 'Austria' || usrcountry == 'Belgium' )

{
Logic
}

As there are more no. of countries coming to use this part, I need to create a custom settings instead of hard coding country value always in this condition.

I have created a hierarchical custom settings let's say test and filled country values like this Australia, New Zealand, Germany by holding them in a custom field under custom settings which are comma separated. However, I am unsure how to use this in if condition of the trigger which I mentioned here - if (usrcountry == 'Australia' || usrcountry == 'New Zealand' || usrcountry == 'Austria' || usrcountry == 'Belgium' )

1 Answer 1

1

See the code sample below for how to use a comma separated list that is stored in a custom setting.

// Get value from the custom setting
MyCustomSetting__c cs = MyCustomSetting__c.getInstance();
String countryList = cs.CountryList__c;

// Split to a List then convert to a Set.
List<String> listOfCountries = countryList.split(',');
Set<String> countries new Set<listOfCountries>;

// Using the Set
if (countries.contains(usrcountry)) {
    // logic
}

Null checks are excluded from this sample. You would need to add them where needed.

2
  • Thanks for your help. Just a small query, when you are saying Null checks which instance you refer to here? Does it the list that you are referring to where the list of countries might be null?
    – SKM
    Commented Jan 15, 2015 at 12:43
  • cs.CountryList__c could return null. I believe MyCustomSetting__c.getInstance() could return null as well but I'm not 100% certain on that.
    – BarCotter
    Commented Jan 15, 2015 at 13:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .