Thomas Riddler
Thomas Riddler

Reputation: 393

Get a list of API Names of all Fields for an Object.

Is it possible to get a list of all API Names for all fields on an object?

Schema.DescribeSObjectResult r =Object__c.sObjectType.getDescribe();
List<String>apiNames =  new list<String>();
for(Schema.DescribeSObjectResult result : r){
   apiNames.add();   //this is where I am lost. 
}

Upvotes: 3

Views: 14225

Answers (2)

ASHISH BURNWAL
ASHISH BURNWAL

Reputation: 1

There is a generic solution too

String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
List<Schema.SObjectField> lstFieldApiNames = fieldMap.values();
system.debug('fieldName->>'+lstFieldApiNames);

Upvotes: 0

Daniel Ballinger
Daniel Ballinger

Reputation: 13537

You can use the fields method to get the Schema.SObjectTypeFields.

Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();
List<String>apiNames =  new list<String>();
for(string apiName : r.fields.getMap().keySet()){
   apiNames.add(apiName);
}
System.debug(apiNames);

Upvotes: 6

Related Questions