Forcetrails: FIELDS() function SOQL examples


In Spring 2021, Salesforce announced a new FIELDS() function in SOQL. Using this function in SOQL, we can select some predefined sets of fields in SOQL queries. Using this FIELDS() function we can query

  • all fields on an Object (Just like SELECT * in an SQL database.
  • custom fields - all the custom fields on an Object.
  • standard fields - all the standard fields on an Object.


The FIELDS() function is supported in 
  • Apex - Only *bounded i.e. FIELDS(STANDARD) is supported as of now.
  • REST API
  • SOAP API
  • Bulk API - Only *bounded i. e. FIELDS(STANDARD) is supported as of now.
  • Salesforce CLI
* Bounded Queries - Where we have a well-defined set of fields, for example, all standard fields.
* Unbounded Queries - Where we have a set of fields that are not predetermined, for example, all fields or all custom fields.
As of now Spring 2021, to be able to use FIELDS(ALL) and FIELDS(CUSTOM) you must limit the rows, maximum limit is 200.

Examples of FIELDS() clause in SOQL

Query all fields

SELECT FIELDS(ALL) FROM Account LIMIT 200


Query all Standard fields

SELECT FIELDS(STANDARD) FROM Account


Query all Custom fields

SELECT FIELDS(CUSTOM) FROM Account LIMIT 200


Mix standard fields and FIELDS(CUSTOM)

SELECT Name, FIELDS(CUSTOM) FROM Account LIMIT 200


Mix custom fields and FIELDS(STANDARD)

SELECT Custom_Field__c, FIELDS(STANDARD) FROM Account



Advantages of FIELDS() function

  • Cleaner and simple code as you don't have to write all fields in the query itself.
  • Developers can easily explore the fields in the object by running the query, without going to the object manager.
  • Integrations with third parties can be loosely coupled as we don't need to change the query in order to add new fields in the query.
  • Easily Scalable as we don't need to update queries to add more fields.
  • Enforces the security of the data being retrieved.


Limitations of FIELDS() function

  • Unbound queries are not yet supported in Apex and Bulk APIs.
  • Unbound queries need to limited up to 200 rows.
  • We can't create a custom group of fields.
  • Need to take care of duplicate fields selected.

No comments :
Post a Comment

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. Sorry for the inconvenience but we have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, reach email us at rahul@forcetrails.com