Reputation: 393
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
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
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