0

I have an object with a lot of fields, is there a method in apex which returns a list of the object's fields?

3

You can use the Apex Describe to do this:

Schema.DescribeSObjectResult oppDescribe = Opportunity.sObjectType.getDescribe();
Map<String, Schema.SObjectField> fields = oppDescribe.fields.getMap();
for (Schema.SObjectFIeld field: fields.values())
{
  //All field info you need is in here.
}
| improve this answer | |
3

Yes, you can do that by using Schema.describeSObjects(List) method.

// sObject types to describe

String[] types = new String[]{'Account','Merchandise__c'};

 // Make the describe call
Schema.DescribeSobjectResult[] results = Schema.describeSObjects(types);

  System.debug('Got describe information for ' + results.size() + ' sObjects.');

// For each returned result, get some info
for(Schema.DescribeSobjectResult res : results) {
System.debug('sObject Label: ' + res.getLabel());
System.debug('Number of fields: ' + res.fields.getMap().size());
System.debug(res.isCustom() ? 'This is a custom object.' : 'This is a standard object.');
// Get child relationships
Schema.ChildRelationship[] rels = res.getChildRelationships();
if (rels.size() > 0) {
    System.debug(res.getName() + ' has ' + rels.size() + ' child relationships.');
}
 }

Refer this question as well Apex: Dynamically get fields for an SObject - no hardcoding

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.