How to fetch all fields of an SObject using Apex in Salesforce?
Dynamic Apex

I am a Software Engineer currently working as Salesforce Developer with hands-on experience in Apex development along with declarative development.
Search for a command to run...
Dynamic Apex

I am a Software Engineer currently working as Salesforce Developer with hands-on experience in Apex development along with declarative development.
No comments yet. Be the first to comment.
Have you ever wondered how software engineers manage to stay on top of the ever-evolving tech landscape? Spoiler alert: It’s not by memorizing everything. Most of my friends often ask me, “Why do you spend your weekends creating content? Isn’t it bet...

Getting SObject Record Type Info in Salesforce with Apex

Post Install Script in Salesforce

Accessing Custom Label in Salesforce Lightning Aura Component

Develop native Android apps using Salesforce Mobile SDK

In this article, we will look at a code example that demonstrates how to retrieve all fields of an Account object using Dynamic Apex.
Dynamic Apex is a feature in the Salesforce platform that enables developers to write Apex code that is capable of dynamically discovering metadata, such as fields and objects, at runtime. This feature provides the flexibility to write generic code that can work with any object and retrieve any data that is available in the Salesforce platform.
We can use the getGlobalDescribe() method of the schema namespace which returns a map that represents the relationship between all SObject names (keys) to SObject tokens (values). Below is the code snippet for the same.
Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Remember the map is generated at runtime on the SObjects currently available for the organization, based on permissions.
In the above snippet, the getGlobalDescribe method returns a map of all the objects in the Salesforce platform, and we can use it to retrieve the SObjectType for the Account object by passing the object name as a string argument.
Schema.SObjectType sobjType = gd.get('Account');
We use the getDescribe() method on the SobjectType to retrieve the metadata for the Account object. The getDescribe() method returns a DescribeSObjectResult object.
Schema.DescribeSObjectResult r = sobjType.getDescribe();
The DescribeSObjectResult has a property called fields that contains a map of all the fields for the object.
Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
We then loop through the field map using a for loop and retrieve the SobjectField for each field using the getMap() method. We then use the getDescribe() method on the SObjectField to retrieve the metadata for each field.
Finally, we output the field name using the getName() method on the DescribeFieldResult object.
for(String fieldName : MapofField.keySet())
{
Schema.SObjectField field = MapofField.get(fieldName);
Schema.DescribeFieldResult F = field.getDescribe();
System.debug('Field Name: '+F.getName());
}
With this code, you can retrieve all fields of an Account object dynamically, without having to hard-code the field names. This can be useful when you want to write generic code that works with any object in the Salesforce platform.
Here is the complete code snippet which fetches all field of the Account Object.
Map <String,Schema.SObjectType> gd = Schema.getGlobalDescribe();
Schema.SObjectType sobjType = gd.get('Account');
Schema.DescribeSObjectResult r = sobjType.getDescribe();
Map<String, Schema.SObjectField> MapofField = r.fields.getMap();
for(String fieldName : MapofField.keySet())
{
Schema.SObjectField field = MapofField.get(fieldName);
Schema.DescribeFieldResult F = field.getDescribe();
System.debug('Field Name: '+F.getName());
}
Also, check out the below video on How to fetch all fields of any sobject in Salesforce using Apex.
Dynamic Apex provides a powerful tool for Salesforce developers, enabling them to write dynamic and flexible code that can work with any object and data in the platform. The ability to retrieve field metadata at runtime can be used in a variety of applications, such as creating dynamic reports or generating dynamic forms.
If this article helped you then consider buying me a coffee.
I am looking to get into technical writing so if you want me to write an article then please connect with me on my Twitter handle here. Let me also know which topic I should write about next.
Learning Salesforce then do checkout Salesforce Verse: https://salesforceverse.com