Code does not work on client script but gives correct answer on Scripts -Background.

Amruta007
Tera Contributor

I wrote a code to count the number of ordered item with a specific choice selected . It gives correct answer on Script background but not on client Script - on Load. I have debugged that count.variables.datum is not functioning on client script. Could you please help me and tell me , what needs to be done here.  This is the code 

 

 

var count = new GlideRecord('sc_req_item');
count.query('cat_item','dd71a4b0471061100aaf2c44846d4320');
var bmwone=0;
var bmwtwo=0;
var othernumber=0;
var recCount = 0;

while (count.next())

{
recCount=recCount+1;
var yt= count.variables.datum;
gs.log(+yt);

if (yt=="01/06/2023")
{bmwone = bmwone+1;
}
else if(yt=="01/10/2023")
{
bmwtwo = bmwtwo+1;
}
else
othernumber = othernumber+1;
}

3 REPLIES 3

-O-
Kilo Patron
Kilo Patron

Like any webpage, SN too is composed of two major parts: the web server and the browser.

Also called server side and client side - in SN parlance.

While both run JavaScript, the two environment are very different:

- the server side support special ServiceNow specific functionality (APIs), while

- the client side, of course, supports standard browser functionality (and APIs).

Thus in server side there is no access to any browser specific classes and objects (e.g. document, window, DOM, etc).

On client side there is no access to any server side specific APIs, e.g. GlideRecord, GlideDateTime, GlideSystem, etc.

Yes, there is a GlideRecord object on client side too, but except for the name, the two have absolutely nothing to do with each other.

The server side variant is working directly with the underlying MariaDB database, while the one on the client side (which is sorta' deprecated too) is basically an XMLHttpRequest wrapper.

All in all it is absolutely expected that code that works on server side will not work on client side, and vice-versa, except if the code only contains code that does not use any ServiceNow server specific or browser environment specific objects and APIs.

There is also a version difference. Server side there is a (way) older version of JavaScript engine used. Client side exists whatever the newest engine the browser provides. That means that server side arrays lack even such methods as find, findIndexOf, etc. It's an almost ES5 with some ES6 features.

I hope it is clear that Scripts - Background runs on ServiceNow server in server side.

As for the original question of what to do: it depends on what you are trying to achieve.

If the data is needed client side, but will never change during the life of the page, one could send the data along with the page load as default values to fields. If there is a need to load data as the page is changed by the user, after the page has been loaded, one needs to do what all webpages do in such cases: use XMLHttpRequest. In ServiceNow client script a special object can be used: GlideAjax (which is another wrapper around XMLHttpRequest). It needs a "handler" Script Include server side, one that will read the parameters "sent in" and will send back whatever needs to be sent back. Such script includes must be marked as being Client callable and must be crafted so as to extend the AbstractAjaxProcessor prototype.

Another problem with your script is that it counts things, but does so using JavaScript. It generally a good idea to do as much as possible in the environment where the data lives and as little as possible in the JavaScript environment. For this there is a variant of GlideRecord, called GlideAggregate that can return the data already grouped and counted or summarized.

Let the community know where you need further help and clarifications.

Amruta007
Tera Contributor

thanks for your detailed answer. I figured out a workaround and my problem is solved. Thank you so much . 

You're welcome! 🙂