1

Is there a way to query all fields on an object that have 'Default Value' ticked (and possibly filled in)?

This should include custom fields.

1 Answer 1

4

No, with SOQL it's not possible to access meta data on the object you are quering. Actually, I don't see any reason why would you need working with metadata using SOQL? Nevertheless, you can easily do this with APEX, and then query records if needed. Here is documentation on the classes you may use to obtain necessary information.

  1. Understanding Apex Describe Information
  2. DescribeSObjectResult
  3. DescribeFieldResult

See the example code below:

Schema.DescribeSObjectResult sobjRes = Schema.SObjectType.Account;

Map<String,Schema.SObjectField> fields = sobjRes.fields.getMap();
for(String name : fields.keySet())
{
    Schema.DescribeFieldResult dfr = fields.get(name).getDescribe();

    if (dfr.isDefaultedOnCreate())
    {
        System.debug('Is Field Custom? ' + dfr.isCustom());
        System.debug('Field Name: ' + dfr.getName());
        System.debug('Default Value: ' + dfr.getDefaultValue());
    }
}
1
  • I was asking specifically in soql. Anyway this is perfect, just what i was looking for. Thank you
    – rna7
    Jul 10, 2018 at 10:21

You must log in to answer this question.

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