With Salesforce Spring 21 release, Salesforce has introduced SOQL Fields functions. The FIELDS() function lets us select groups of fields without knowing their names in advance. This function simplifies SELECT statements, avoids the need for multiple API calls, and provides a low-code method to explore the data in the org. This function is available in API version 51.0 and later.

We have three way to get Fields dynamically.
- FIELDS(ALL)—to select all the fields of an object.
- FIELDS(CUSTOM)—to select all the custom fields of an object.
- FIELDS(STANDARD)—to select all the standard fields of an object.
For example we can use below query to get all data
SELECT FIELDS(ALL) FROM Account LIMIT 200
We can follow the . notation to access the fields easily.
for(Account acc: [SELECT FIELDS(Standard) FROM Account LIMIT 200] ) {
System.debug('============='+acc.Name);
}
But we have some limitations here.
- If we try to use FIELDS(ALL) or FIELDS(CUSTOM) in Spring 21 we will get the error “The SOQL FIELDS function is not supported with an unbounded set of fields in this API”.
- LIMIT n—where n is less than or equal to 200.
Bounded and Unbounded Queries
Bounded queries have well-defined sets of fields. While unbounded queries have sets of fields that the API can’t determine in advance. For example, because the number of custom fields for an object is not predetermined, FIELDS(CUSTOM) and FIELDS(ALL) are considered unbounded. Below table shows the support for FIELDS() in bounded and unbounded queries:
Approach | Bounded – FIELDS(STANDARD) | Unbounded – FIELDS(ALL) and FIELDS(CUSTOM) |
---|---|---|
Apex (inline and dynamic) | Supported | Not supported |
Bulk API 2.0 | Supported | Not supported |
CLI | Supported | Supported only if the result rows are limited. |
SOAP API and REST API | Supported | Supported only if the result rows are limited. |
So do we have any workaround here, yes. We can still use the Describe methods to get all fields and can make Dynamic SOQL.
public class ApexUtility {
public static String AllFields(String ObjectName) {
List<String> fields = new List<String>(Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap().keySet());
String query = 'SELECT '+String.join(fields, ',')+' FROM '+ObjectName;
return query;
}
}
for(Account acc: Database.query(ApexUtility.allFields('Account')) ) {
System.debug('============='+acc.Name);
}
We can enhance this utility to apply different filters. Although this will enhance the CPU time in processing.
Check my Instagram page for more updates.
So now you have two ways to get all data without hardcoding field names. Which option you like most, let me know in comments. Happy Programming 🙂