How to get all Field Label or Field Name from Object Using APEX.

You are currently viewing How to get all Field Label or Field Name from Object Using APEX.
How to get all Field Label or Field Name from Object
  • Post author:
  • Post category:Blog
  • Post comments:0 Comments

In this tutorial I have discuss how you can fetch all fields of any standard or custom object in your lighting component. Today I will show you simplest way to get to get all Field Name and Field Label using APEX code.

With the help of Schema class you can easily get Field Names or Field Labels of an salesforce Object .

Let’s see how to use Schema class to get Field Names of an standard Account Object.

Map<String, Schema.SObjectType> detail = Schema.getGlobalDescribe();
for(Schema.SObjectField fields :detail.get('Account').getDescribe().fields.getMap().Values()) {
    System.debug('fields '+ fields.getDescribe().getName());
}

We have use Schema.getGlobalDescribe() which Returns a map of all sObject names (keys) to sObject tokens (values) for the standard and custom objects defined in your organization. And we have used fields.getDescribe().getName() to get API name of Object field.

Similarly you can get Field Label of an Object using fields.getDescribe().getLabel() method .

If you want to return labels and names both at a same time then you can create a Map and put these two to your map like :

mapOfNameAndLabel.put(fields.getDescribe().getName(),fields.getDescribe().getLabel());

Watch the following video where I have explain same concept with more details.

Leave a Reply