SFDC Stop - Always the latest about Salesforce

Full Tutorial Series with videos, free apps, live sessions, salesforce consulting and much more.

Telegram logo   Join our Telegram Channel

Thursday, 28 January 2021

SOQL FIELDS() is now Generally Available | Advantages and Limitations | Salesforce Spring '21 Release | SFDC Stop

Hello Trailblazers,

In this post, I am going to discuss about one of my favorite updates from Salesforce Spring '21 Release (v51.0) for Salesforce Developers which is:

SOQL FIELDS() is Generally Available

This is the best thing I read in this release, no more long SOQL queries to retrieve a lot of fields while integrating with external systems. 

You must be thinking that we don't need to update the apex code again and again to accomodate more fields due to frequent changes in the data model. IS IT REALLY SO? Let's find out.


You can query standard fields by using FIELDS(STANDARD) in the SOQL query:

SELECT FIELDS(Standard) FROM Account

Although most of the fields I queried are NULL :P because I just created some dummy account records using apex, but this is really amazing!!

You can query custom fields by using FIELDS(CUSTOM) in the SOQL query, Make sure to specify a limit as well (at max 200) as shown below:

SELECT FIELDS(Custom) FROM Account ORDER By LastModifiedDate DESC LIMIT 2

You can query all fields standard + custom by using FIELDS(ALL) in SOQL query:


As you can see above, first of all we're getting all the standard fields and at the end we're getting al the custom fields as well.

What CAN be done and what NOT?

1. You cannot have duplicate fields in SOQL query. Have a look at the below query, I am using FIELDS(Custom) which will query all custom fields and I am explicitly specifying Cost__c custom field as well which results in field duplication.

2. You cannot query custom fields using Apex. For Ex: the below code is not valid.

I even tried the above function by using Database.query() the file was saved successfully but I wasn't able to execute the function and got the same error as you can see below:

Although you can query all standard fields with some custom fields explicitly specified as shown below:

It worked perfectly fine as you can see the result:

You'll find the similar behaviour, if you try to use FIELDS(ALL).

Reason: FIELDS(ALL) and FIELDS(CUSTOM) are considered unbounded because the number of fields are not predetermined because the custom fields are created by admins/developers. Learn more about bounded and unbounded limitations here.

3. You must specify a LIMIT while querying unbounded fields i.e. using FIELDS(ALL) or FIELDS(CUSTOM) in the query. The limit can be 200 at max.

4. If you're specifying a set of IDs in the query, that must be a set of 200 ids at most. For ex:

5.  The SOQL query may query less records than specified in the LIMIT while using FIELDS() if your object contains CLOB or BLOB fields. In this case, it's recommended to checkout the number of records returned and update your offset accordingly to get the next set of records. You can use nextRecordsUrl  in case of REST API and queryMore() in case of SOAP API to query more records.

6. If you try to query custom fields for an object that doesn't have any, you'll receive an error as shown below:

Although if you specify even a single standard  field along with this query, the error will be ignored and your query will return the standard fields as shown below:

7. If you don't have access to a field (Field Level Security) it's automatically ignored. If you see in point number 4, the field Cost__c was also queried. However, now, I have removed the access to that field. You can see the result below:

So, that's all for this post everyone, I hope you liked it. I am also hoping that we can get over this bounded and unbounded fields limitation in the future releases so that we can query all custom fields in apex as well. Let me know your feedback about this post and your suggestions in the comments down below. 

Happy Trailblazing..!!

No comments:

Post a comment