[SalesForce] Custom settings in one of the triggers

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' )

Best Answer

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.

Related Topic