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:
- Line 1: We retrieve a map of all
SObject
types available in the Salesforce org usingSchema.getGlobalDescribe()
.- The keys in the map are the API names of the
SObjects
(e.g.,Account
,Contact
), and the values are their correspondingSchema.SObjectType
.
- The keys in the map are the API names of the
- Line 2: From the global describe map (
gd
), we fetch theSchema.SObjectType
for theAccount
object using its API name Account. - Line 3: Using the
sobjType
, we fetch detailed metadata about theAccount
object by callinggetDescribe()
. - Line 4: We retrieve a map of all fields in the
Account
object by accessing thefields
property of the metadata.- The keys in this map are the field API names (e.g.,
Name
,Industry
), and the values areSchema.SObjectField
objects representing the fields.
- The keys in this map are the field API names (e.g.,
- 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.
- Line 6: Fetch the
Key Points:
Schema.getGlobalDescribe()
: Provides metadata for allSObjects
in the org.getDescribe()
: Provides detailed metadata about anSObject
or field.fields.getMap()
: Returns a map of field API names and their correspondingSchema.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.