Fetch API Names of All Fields for an Object Using Dynamic Apex in Salesforce

In the previous article we have learned about How to Fetch all Record Types of an Object using Dynamic Apex in Salesforce which you can read from here. In this article I will tell you How to Fetch API Names of All Fields for an Object Using Dynamic Apex in Salesforce?

In this article, we will look at a code example that demonstrates how to retrieve all fields of an Account object using Dynamic Apex.

Below is the code snippet which will fetch all fields of Account Object.

// Get a map of all SObject types available in the org with their API names as keys.
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();

// Retrieve the Schema.SObjectType for the Account object from the global describe map.
Schema.SObjectType sobjType = gd.get('Account');

// Get detailed metadata about the Account object.
Schema.DescribeSObjectResult r = sobjType.getDescribe();

// Retrieve a map of all fields in the Account object, where the key is the field API name and the value is the Schema.SObjectField.
Map<String, Schema.SObjectField> MapofField = r.fields.getMap();

// Iterate through each field name in the map.
for (String fieldName : MapofField.keySet()) {
    // Retrieve the Schema.SObjectField for the current field.
    Schema.SObjectField field = MapofField.get(fieldName);
    
    // Get detailed metadata about the field.
    Schema.DescribeFieldResult F = field.getDescribe();
    
    // Log the field name for debugging or verification purposes.
    System.debug('Field Name: ' + F.getName());
}

Let’s try to understand the code step-by-step:

  1. Line 1: We retrieve a map of all SObject types available in the Salesforce org using Schema.getGlobalDescribe().
    • The keys in the map are the API names of the SObjects (e.g., Account, Contact), and the values are their corresponding Schema.SObjectType.
  2. Line 2: From the global describe map (gd), we fetch the Schema.SObjectType for the Account object using its API name Account.
  3. Line 3: Using the sobjType, we fetch detailed metadata about the Account object by calling getDescribe().
  4. Line 4: We retrieve a map of all fields in the Account object by accessing the fields property of the metadata.
    • The keys in this map are the field API names (e.g., Name, Industry), and the values are Schema.SObjectField objects representing the fields.
  5. Lines 5-12: We loop through each field in the map:
    • Line 6: Fetch the Schema.SObjectField for the current field using the field name.
    • Line 8: Call getDescribe() on the field to get detailed metadata about it.
    • Line 10: Log the field name using F.getName() for debugging or verification purposes.

Key Points:

  • Schema.getGlobalDescribe(): Provides metadata for all SObjects in the org.
  • getDescribe(): Provides detailed metadata about an SObject or field.
  • fields.getMap(): Returns a map of field API names and their corresponding Schema.SObjectField.
  • getName(): Returns the field’s name for logging purposes.

Also, check out the below video on How to fetch all fields of any sobject in Salesforce using Apex.

We hope now you know How to Fetch API Names of All Fields for an Object Using Dynamic Apex in Salesforce. If you have any doubts then let us know in the comment box below.

Leave a Comment