2

So in the documentation for the DescribeFieldResult.getDefaultValue method, it says:

Returns the default value for this field

There is a related method called getDefaultValueFormula whose description reads:

Returns the default value specified for this field if a formula is not used.

What is the difference between these methods? I have a field with a default value. When I call getDefaultValue, I get null. When I call getDefaultValueFormula, I get back "This is the default value" (with quotes). I don't understand why getDefaultValue returns null.

6
  • The method is broken for some types DescribeFieldResult.getDefaultValue() returns null for Checkbox field and Picklist field though works for strings.
    – Keith C
    Commented Oct 26, 2015 at 23:48
  • @KeithC This is just a text field with a default value specified.
    – Adrian Larson
    Commented Oct 26, 2015 at 23:52
  • That default value for your text field is probably placed in that formula editor in quotes? Commented Oct 27, 2015 at 0:02
  • @cricketlang Yes, exactly. That is the only way I am currently aware of for specifying a default value.
    – Adrian Larson
    Commented Oct 27, 2015 at 0:25
  • It's technically a formula, thus why you have to use the formula describe to get it. It's super weird. It's the way Salesforce handles default values. If it is calculated once at record creation, it will pull up under getDefaultValue(). If it's a calculation done at query time, the formula one works (like now() or text values). Commented Oct 27, 2015 at 0:28

3 Answers 3

3

I ended up going with a dummy object (detailed here) to give me default values. It does depend on having read access to the fields in question:

My_Object__c defaults = (My_Object__c)My_Object__c.sObjectType
    .newSObject(/*recordTypeId*/ null, /*loadDefaultValues*/ true);
2

Base on the comment thread, I just looked in some code I use to default custom settings and that is using getDefaultValueFormula; this was written several years ago:

// Presently only works for strings
private void defaultFromDescribe(SObject sob, SObjectField[] fields) {

    // Fill in defaults values for the newly added fields
    for (SObjectField field : fields) {
        DescribeFieldResult df = field.getDescribe();
        // Default methods are pretty weak and this is the one that (partially) works
        String defaultValue = df.getDefaultValueFormula();
        if (defaultValue != null) {
            defaultValue = removeQuotes(defaultValue);
            sob.put(field, defaultValue);
        }
    }
}

private String removeQuotes(String s) {

    if ((s.startsWith('"') && s.endsWith('"')) || (s.startsWith('\'') && s.endsWith('\''))) {
        return s.substring(1, s.length() - 1);
    } else {
        return s;
    }
}
3
  • This is definitely what first came to mind. I don't really like the string manipulation aspect, especially if you have some combination of string and formula like "Hello " & $User.Name & ", it is " & TEXT(NOW()).
    – Adrian Larson
    Commented Oct 27, 2015 at 17:17
  • @AdrianLarson Agreed. +1 for your own answer.
    – Keith C
    Commented Oct 27, 2015 at 17:36
  • Also, I find abbreviating to sob quite humorous.
    – Adrian Larson
    Commented Oct 27, 2015 at 17:39
1

I have raised support case since I couldn't get default value for Time field. Here's Salesforce response:

I have received the below update from my next level.

An investigation is raised regarding the issue and the Product Team has confirmed that this is WAD(Working As Designed) and not a bug.

They have informed that for the data type that default value is defined as Formula, getDefaultValue() returns null and getDefaultValueFormula() returns configured formula value.

They have also confirmed that if the data type's default value is defined as Formula, getDefaultValue() returns null

Here is the investigation W-2423865 been raised and you can refer the investigation number for your reference

You must log in to answer this question.

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