function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Fabio ScheurelFabio Scheurel 

Use the default value for a list custom setting

Hi there,

I was wondering if the default value that I can specifiy for my fields on a list custom setting is usable for some kind of fallback. I would like to retrieve the default value if the stored value for a data set is NULL or a data set does not exist.

I know that I can retrieve the default formula with this call:
Labels__c.TextKeepInTouchDescription__c.getDescribe().getDefaultValueFormula()
However, it will just return a formula, but I need the evaluated result of that formula. Something like
String default = new Labels__c().TextKeepInTouchDescription__c;
just leaves me with a null value.

Any idea?
pconpcon
I'm not certian this is the "right way" but I can tell you what we do around custom settings.  I have a utility class that handles all of my custom setting requests.  This utility method takes in a setting name and then returns a default value if the value is null or if the setting does not exist

public class MyCustomSettingUtils {
     public static DEFAULT_DESC_VALUE = 'This is the default';

     public static MyCustomSetting__c getMyCustomSetting(String settingName) {
          MyCustomSetting__c result = MyCustomSetting__c.getValues(settingName);
          return result;
     }

     public static getDescription(String settingName) {
          MyCustomSetting__c setting = getMyCustomSetting(settingName);
          if (setting == null || setting.MyDescription__c == null) {
               return DEFAULT_DESC_VALUE
          }

          return setting.MyDescription__c;
     }
}

NOTE: This code has not been tested and may contain typographical or logical errors
Fabio ScheurelFabio Scheurel
Your're hardcoding the defaults in your utility class. That doesn't help me keeping the system flexible. I found out that the field defaults are only applied when saving a custom setting data set with null values. As you cannot store a null value for Strings/Text via the UI, just empty strings, it won't give me a handy way of dealing with defaults. Still thinking of a good solution, including deployment. Thanks anyway. Regards Fabio