shubham lashkan
Shubham Lashkan

Follow

Shubham Lashkan

Follow
Dynamic SOQL in Apex

Dynamic SOQL in Apex

Fetch all fields of a record using Dynamic SOQL

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

3 min read

Play this article

Table of contents

  • List All Fields
  • Store API Names in List Of String
  • Create SOQL Query String
  • Execute Query using Database Method
  • Conclusion

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

In the last article, you read about fetching all fields of an SObject using dynamic apex in Salesforce. You can have a look at that article from here because most of the code will be similar.

List All Fields

If you remembered in the last article we wrote the below code to fetch API names of all fields of Account SObject.

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

Store API Names in List Of String

Let us make the changes in the above code to store API Names of fields of Account Object.

List<String> APINames = new List<String>();
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();
APINames.add(f.getName()); 
}

As you can see in the above code snippet I have defined a list of strings named APINames which stores API Names of all fields. Also, I have removed the debug logs from the for loop as it is not required as of now.

Create SOQL Query String

Now let's use the API names of fields to create a SOQL Query and store it in a String.

List<String> APINames = new List<String>();
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();
APINames.add(f.getName()); 
}
String fetchAllAccount = 'Select '+String.join(APINames, ',') + ' from Account' ;

As you can see in the above code snippet the last line creates a SOQL query to fetch all fields of account records.

The Join method of String Class Joins the elements of the specified iterable object, such as a List, into a single String separated by the specified separator.

In our code example, we have separated the string with a comma to construct the SOQL query.

Execute Query using Database Method

As we have already constructed the SOQL query now let's execute the query using the Database.Query method.

List<String> APINames = new List<String>();
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();
APINames.add(f.getName()); 
}
String fetchAllAccount = 'Select '+String.join(APINames, ',') + ' from Account' ;
list<Account> accountList = Database.query(fetchAllAccount);
system.debug(accountList);

Execute the above code snippets from an anonymous window and open the debug logs. You will see list of account records with values of all fields being displayed.

Conclusion

The concept looks more or less the same as we have to fetch the metadata information of an object so that we can construct the SOQL query and execute the query using Database.Query method.

I hope i am able to explain you how to write a simple dynamic SOQL query to fetch all field value of object record.

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