Fetch All Fields In A SOQL Query At Once - Spring '21

Fetch All Fields In A SOQL Query At Once - Spring '21

Ever wanted to fetch all fields in a SOQL query at once and got frustrated doing all the research to get hold of the API names? Especially for the custom fields? You are not alone.

With this release, we can use FIELDS(ALL), FIELDS(STANDARD), FIELDS(CUSTOM) in the SOQL query and this is what happens.

  • FIELDS(ALL) - This fetches all the fields of an object at one go. This is similar to * operator in SQL query.
  • FIEDLS(STANDARD) - This can be used to fetch all the standard fields of an object at one go.
  • FIELDS(CUSTOM) - This is use to fetch all the custom fields alone on an object.

Sweet isn't it?

However, there is a limit, we cannot fetch more than 200 records per query.

This is how the full blown query looks like.

List<Account> accountsList = [SELECT FIELDS(ALL) FROM Account LIMIT 200];

With the usual queries, where we specify all the field names laboriously, a call will be made to the server using the Describe class, all the fields are fetched and from the list of the fields returned back, based on the fields we have specified the query will ne formed and invoked.

Now we dont have to do the whole song and dance.