1

Im trying to pull in the default value set in the configuration for a Long Text Area field we created on the Case object. How do we retrieve that value through Apex? The following returns a "null".

Schema.DescribeFieldResult f = Schema.sObjectType.Case.fields.CustomField__c;
f = f.getSObjectField().getDescribe();
System.debug('Default Value is: ' + f.getDefaultValue());

The debug returns "null", so

"Default Value is: null".

4
  • 1
    What is wrong with the code you have? What's not working? Also, what is the default value set to? Commented Jan 16, 2020 at 20:29
  • Thank you for the response. The debug returns "null", so "Default Value is: null". It just has some standard text in it that the user will do multiple things with, including copying and pasting multiple versions of that same text in that field.
    – user891859
    Commented Jan 16, 2020 at 20:54
  • Is it plain text or a formula? Commented Jan 16, 2020 at 20:55
  • Mostly text, but uses the BR() function for line breaks. So a formula.
    – user891859
    Commented Jan 16, 2020 at 20:57

1 Answer 1

2

The documentation is unclear in this, but here go two options:

  1. You can try .getDefaultValueFormula(). The documentation for this is "glorious":

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

    But what it means that it will return the value with the formula configured.

  2. Alternatively, and I repurposed @AdrianLarson's answer from here, you can do this:

    Case defaults = (Case) Case.sObjectType.newSObject(/*recordTypeId*/ null, /*loadDefaultValues*/ true);
    
    System.debug(Case.CustomField__c);
    

    I don't thing it performs as well but should do the trick

3
  • I was wondering if we should flag as a dupe.
    – Adrian Larson
    Commented Jan 16, 2020 at 21:11
  • It’s marginal. I think there might be enough difference with your original post that merits its own question. Commented Jan 16, 2020 at 21:15
  • This worked perfectly. Thank you both! I used #2.
    – user891859
    Commented Jan 17, 2020 at 15:04

You must log in to answer this question.

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