salesforceschemaapex-code

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. 
}

Solution

  • 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);