FIELDS() in SOQL: How to Query All Fields in SOQL

In this post, we will go through the latest feature that is introduced in Spring ’21, the feature all Salesforce Developers have been waiting for: To Query All Fields in SOQL in Salesforce. Yes, you heard it right.

Ever since we got introduced to SOQL in Salesforce, we always asked, why can’t we query all the fields in SOQL using * ?, just like we do in the Standard Query Language (SQL). Well, we can finally do it.

FIELDS() in SOQL: How to Query All Fields in SOQL
FIELDS() in SOQL: How to Query All Fields in SOQL

Earlier Approach

Until now, to query all Fields in SOQL, we first need to make a getDescribe() call on the respective SObject to get a Map of all the Fields. Then, we had to create a list of Fields from this Map. And finally, we had to create a Dynamic SOQL query using join() and query the records using Database.query(). It would look something like below, assuming we are querying all Fields of Account:

// Get the Map of Schema of Account SObject
Map<String, Schema.SObjectField> fieldMap = Account.sObjectType.getDescribe().fields.getMap();
          
// Get all of the fields on the object
Set<String> setFieldNames = fieldMap.keySet();
list<String> lstFieldNames = new List<String>(setFieldNames);
         
// Dynamic Query String.
List<Account> lstAccounts = Database.query('SELECT ' + String.join(lstFieldNames, ',') + ' FROM Account');

Not anymore though.

Query All Fields in SOQL

The new FIELDS() function in SOQL lets you select all the Fields without knowing their names in advance. This FIELDS() function can accept three parameters:

  • ALL: To query All Fields.
  • STANDARD: To query all Standard Fields.
  • CUSTOM: To query all Custom Fields
SELECT FIELDS(ALL) FROM Account LIMIT 200
SELECT FIELDS(STANDARD) FROM Account
SELECT FIELDS(CUSTOM) FROM Account LIMIT 200

Also Read : Safe Navigation Operator – Avoid Null Pointer Exceptions

This is how we can query all the Fields in SOQL in Salesforce.

FIELDS() in SOQL: Consideration

You must put a LIMIT clause with a maximum of 200 for ALL and CUSTOM variation of FIELDS() function. Otherwise, you will get the below error:

The SOQL FIELDS function must have a LIMIT of at most 200.

FIELDS() in SOQL: Performance

For the previous versions of SOQL, we have to specify all the fields in the SOQL query that we wanted to retrieve. This requires an API call to describe the object to get the list of Fields and construct the SOQL query that we did earlier in the first example. It takes a lot of Effort and Time. Also, such a query could exceed the query character limit for large complex queries that retrieve lots of data. The FIELDS() function allows us to select all the fields without knowing their names in advance. This also eliminates a round-trip to the server to prepare a SOQL statement, reduces a lot of typing, and simplifies query statements.

That is all from this post. If you don’t want to miss new implementations, please Subscribe here.

Also Read : Send Custom Notification using Apex in Salesforce

Check official documentation on How to Query All Fields in SOQL in Salesforce from the Spring ’21 release notes.

Thank You!

Leave a Reply

Your email address will not be published. Required fields are marked *