Script Function Available to BOTH Client side and Server side Code?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2023 05:35 AM
Is it possible to write a JavaScript function, and make that function available to BOTH client-side and server-side code?
(I don't want to copy it and create a duplicate maintenance situation.)
How? Where? Thanks!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2023 05:56 AM - edited 09-30-2023 06:30 AM
Hi @G24
Unfortunately It is not possible to write a script which can be accessible from both Server Side and Client Side.
Some might say Client Callable Script Include can be used on Client Side, Technically and Practically it is not correct, it still executes Server Side we are just consuming it in Client Side through AJAX.
Anvesh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2023 06:23 AM
Hi @G24 ,
We can't directly call the same script include from Server and client both but you can have option to call script include within different script include using gs.include()
Below link might help you to understand better.
https://www.basicoservicenowlearning.in/2019/12/script-include-servicenow.html?m=1
ServiceNow Developer
I know one thing, and that is that I know nothing.
- Socrates
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2023 06:42 AM
I would question the assumption(?) that a Script Include with the Client callable box checked cannot be called from the server-side, like from a Business Rule. I don't know that I've tried this, but I have used the same SI and function from a reference qualifier where a current.field/variable name is passed as an argument, and from a Client Script passing parameters to the server. In this case the function just needs to use the arguments, and if they're blank assign from the client parameters.
getCIsByClassAndLocation: function(loc,prod) {
if (!JSUtil.nil(prod)) {
var product = prod;
} else {
var product = this.getParameter("sysparm_product");
}
if (!JSUtil.nil(loc)) {
var location = loc;
} else {
var location = this.getParameter("sysparm_location");
}
...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2023 11:20 AM
I just tested a Client callable Script Include, calling a function via GlideAjax in a Catalog Client Script, passing in the sys_id of a user, then I called the same SI and function from a Business Rule with current.opened_by as an argument and the script executes as expected, using the above logic to assign the arguments or parameters to script variables. Is this what you meant by making a function avail to both client-side and server-side code?