shubham lashkan
Shubham Lashkan

Follow

Shubham Lashkan

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

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

Dynamic Apex

shubham lashkan's photo
shubham lashkan
ยทFeb 11, 2023ยท

3 min read

Play this article

Table of contents

  • What is Dynamic Apex?
  • How to access All SObjects?
  • Get Account SObject
  • Let's retrieve Account Object Metadata
  • Fetch all fields of the Account
  • List all fields
  • Conclusion

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

What is 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.

How to access All SObjects?

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.

Get Account SObject

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

Let's retrieve Account Object Metadata

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

Fetch all fields of the Account

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

List all fields

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()); 
}

Conclusion

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.

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.

Did you find this article valuable?

Support shubham lashkan by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this