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 Mar 12 '18 at 15:13
  • 1
    The linked question doesn't really seem to have anything to do with what you are asking here. – Adrian Larson Mar 12 '18 at 15:23
  • @adrian-larson Yes, sorry, had a couple of links and copy-pasted the wrong one. – frackham Mar 13 '18 at 8:20
  • @sfdcfox yes. Adrians answer below looks perfect. – frackham Mar 13 '18 at 8:20
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());
}

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.