SOQL FIELDS() | How and where to use it ? Limitations ? | Salesforce ☁️⚡️


SOQL FIELDS()

A familiar productive way to explore the shape of your data

So in the Spring 21 release salesforce has introduced the field() in SOQL. So let's task about it !

If you are familiar with MS SQL or MY SQL so you must have seen this already in it. In MY SQL or MS SQL we can use * to retrieve all records from the object or table.

I.E. SELECT * FROM ObjectName

Now we are having same functionality in the SOQL also. We can use fields(All) to get all records from a particular object. Let's say if you want to get all records from Account so you can write a query like 

SELECT FIELDS(ALL) FROM Account

It will return all the custom and standard fields as a result.


Why we need it ?

Let's understand it with an example. Let's say you are working as a salesforce admin or developer for a school and if the requirement is to have all records of a particular class from the custom object students__c. For that you need to understand the shape of the object first than may be you will explore the fields in the object manager. Than you have to write a query which will include all the fields from that object. 

If you are having a large set of fields than it could be a pain to write all the fields in the query.

But with fields() function now you can directly write the query likeb :  

SELECT FIELDS(ALL) FROM students__c  and the query will return all the fields from the object. It's pretty cool right !

Is it different from the MY SQL and MS SQL * sign ? 

The answer is yes, in the field function we can select standard and custom fields separately. Let's take the previous example so if the requirement is to get all the standard fields from the students__c object than the query will be 

SELECT FIELDS(STANDARD) FROM students__c.

And if you need all custom fields from the students__c object than you can simply write the query

SELECT FIELDS(CUSTOM) FROM students__c 


FIELDS() supports Bounded & Unbounded queries also : 

I hope now you understand how and where you can use fields, but before using it please make a note of some important points : 

Notes :

1. FIELDS() can cause errors if you use it with operators that require aggregation.

For example if we want to calculate minimum marks for a student from our students__c object the query will be 

SELECT Id,  MIN(Marks__c) FROM Students__c GROUP BY Id

But adding FIELDS() to the query like this

SELECT FIELDS(ALL), MIN(Marks__c) FROM Students__c GROUP BY Id LIMIT 200

We can not make it GROUP BY FIELDS(ALL)


2. If there are any fields in the field list that do not exist, there is no error. For example, this query returns status code 200 (even if the object contains no custom fields):

SELECT Id, FIELDS(Custom) from students__c limit 200


3. The list of fields returned by FIELDS() reflects the current state of the org's metadata and data model. So clients must be prepared to accept different results as the metadata and data model changes. These changes can also affect the performance of the query.

Checkout complete video below 

 If you have any question please leave a comment below.

If you would like to add something to this post please leave a comment below.
Share this blog with your friends if you find it helpful somehow !

Thanks
Keep Coding 


Post a Comment

0 Comments