0

So, using this I can get populated fields and values for a given sObject:

How to get the Runtime-Type of an Object dynamically (for Primitive Data Types and SObjects)

...and I have this working in the debug log.

enter image description here

However, is there any way to get this for all fields, whether populated or not? (Effectively describing the object given a generic sObject).

4
  • 1
    So you want a list of fields for an object?
    – sfdcfox
    Commented Mar 12, 2018 at 15:13
  • 1
    The linked question doesn't really seem to have anything to do with what you are asking here.
    – Adrian Larson
    Commented Mar 12, 2018 at 15:23
  • @adrian-larson Yes, sorry, had a couple of links and copy-pasted the wrong one.
    – frackham
    Commented Mar 13, 2018 at 8:20
  • @sfdcfox yes. Adrians answer below looks perfect.
    – frackham
    Commented Mar 13, 2018 at 8:20

1 Answer 1

6

If you know the SObjectType already:

public static List<SObjectField> getFields(SObjectType sObjectType)
{
    return sObjectType.getDescribe().fields.getMap().values();
}

If you just have an SObject, you can get its token and pass it to the above:

public static List<SObjectField> getFields(SObject record)
{
    return getFields(record.getSObjectType());
}

You must log in to answer this question.

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