Get list of fields for History object via APEX

apexschema

I need to dynamically retrieve the list of fields for history objects, whether it be Asset or Opportunity etc. I normally use the following code to retrieve these fields for standard and custom objects:

String SobjectApiName = 'Asset';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

String apiNames = '';

for(String apiName : fieldMap.keyset())
{
    if(fieldMap.get(apiName).getDescribe().isUpdateable()) {
        apiNames += apiName + ',';
    }
}

system.debug(apiNames);

But this does not work for History objects.

How can I retrieve the list of field api names for say, "AssetHistory"?

Best Answer

When you collect the fields; you have a check isUpdateable; where you filter out the fields which are allowed to be updated by user.

As all fields on *History object (AssetHistory here) are read only, you do not see any fields in result.

Not sure what you are trying to do, removing isUpdateable condition will show you all the fields.