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.

  • The method is broken for some types DescribeFieldResult.getDefaultValue() returns null for Checkbox field and Picklist field though works for strings. – Keith C Oct 26 '15 at 23:48
  • @KeithC This is just a text field with a default value specified. – Adrian Larson Oct 26 '15 at 23:52
  • That default value for your text field is probably placed in that formula editor in quotes? – cricketlang Oct 27 '15 at 0:02
  • @cricketlang Yes, exactly. That is the only way I am currently aware of for specifying a default value. – Adrian Larson Oct 27 '15 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). – cricketlang Oct 27 '15 at 0:28
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;
    }
}
  • 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 Oct 27 '15 at 17:17
  • @AdrianLarson Agreed. +1 for your own answer. – Keith C Oct 27 '15 at 17:36
  • Also, I find abbreviating to sob quite humorous. – Adrian Larson Oct 27 '15 at 17:39
2

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

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.