Querying All Fields in SOQL

We all know in SQL we use * to query all fields from database, however till now we didn't have anything similar in SOQL for developers. Hence we had to either use getDescribe() method from Apex and then use that in the Dynamic SOQL query to query all fields or we had to get all the API names of the fields and place it comma separated in the SOQL query.


However, now we can change that. We no longer need to know any API names and also don't require any unnecessary apex code to get fields in query.

The new FIELDS() function in SOQL lets you select all the fields. This function accept 3 parameters as described below.

  1. ALL: To query for all the fields (Need to add LIMIT clause)
  2. STANDARD: To query for all standard fields
  3. CUSTOM: To query for all custom fields (Need to add LIMIT clause)

Below is the example of the queries.
  1. SELECT FIELDS(ALL) FROM ACCOUNT LIMIT 200
  2. SELECT FIELDS(STANDARD) FROM ACCOUNT 
  3. SELECT FIELDS(CUSTOM) FROM ACCOUNT LIMIT 200
Note: 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
Please check the official salesforce documentation from here.

Comments

Most Popular

Upsert with External Id in Salesforce

Gamification : Assigning Badges to Community Users based on user's Reputation point

Calling an Apex Method From LWC