query() - What we do not know

           


Do you know that query() method of GlideRecord object accepts parameter as well. Many of us may say that ofcourse I know at client side query() methods accept callback function as a parameter which makes it asynchronous. That's right but is that all, what about server side ?

Recently I was going through servicenow docs and was surprised to see that at server side also query() method accepts parameter and that parameter is:  'filter condition'. Yes, you read it right, it takes filter condition as a parameter. Although I couldn't find any documentation around it, so did some searching around it and let me tell you all that I could find, how to use it, where it can be used, and it's limitation.

At server side query() method accepts two paramaters first the field name and second the field value. 

Let's see an example:

var gr= new GlideRecord("incident");
gr.query("priority","2")
gs.print(gr.getRowCount())
while(gr.next()){
    gs.print(gr.number + "  -  " + gr.getDisplayValue("priority"))
}





So, one might think does it mean it can replace addQuery() method as we use addQuery() for the same purpose ? Absolutely not, here comes the limitation of this method.

Below are the limitations of parameterized query() method :

1. It takes just two parameters, so if your query needs multiple conditions then it can't be used.

2. Since it just takes two parameters in the form of field-value pair, you can't use any operator. It defaults search with the '=' operator between field-value pair. There is no scope for any other operators to be used.

So, at server side you can use parameterized query() method, when you need to query based on any single field value.

Okay, does the same thing work at client side as well ?? Yes, but at client side surprisingly it has additional features embedded.

At client side there is scope of multiple queries i.e. you can include multiple conditions as well, unlike server side there is no limitation to the parameter length, only constraint is it needs to be in the form of field-value pair. 

Let's see an example:

var gr= new GlideRecord("incident");
gr.query("priority","2","active","false");


Now, how this is processed at client side is, first it checks if the parameter of query() method is a callback function i.e. if type of the parameter is a function if yes, then it processes accordingly. If it is not a callback function, then it tries to parse it as a condition in the sequence of field-value pair. Above written script will parse the query parameter in conditon of length 2 as shown below :





At client side also the limitiation with query() method's parameter is same, it accepts field-value pair only, so there is no scope for any other operator to be used. Default operator used is '='


Note : It is highly recommended that GlideRecord at client side should not be used. It impact the instance performance and end user experience.


I hope this article may have helped you learn something new and interesting.

Happy learning.

Regards,
Kamlesh








Comments

  1. Can we add an encoded query in query() method?

    ReplyDelete
    Replies
    1. No, as discussed, it strictly takes two parameter in field-value pair. Encoded query won't qualify hence can't be used.

      Delete

Post a Comment

Popular posts from this blog

How to create custom probe and sensor.

Discovery Probes and Sensors

The Year changer - YYYY