The CreatorCon Call for Content is officially open! Get started here.

Can a Script Include be called by the client AND server?

MBarrott
Mega Sage

Thinking of expanding a Script Include into a Utils but right now it's just being utilized server side.

 

If I modify the Script Include so that it is client callable, can the server side still be called successfully? 

1 ACCEPTED SOLUTION

Robbie
Kilo Patron
Kilo Patron

Hi @MBarrott,

 

It sure can.

Rather than reinvent the wheel - check this great article written @Earl Duque (Kudos) who provides perfect examples of the 'how' this is achieved and the syntax - see below link.

 

But to answer your question directly - Yes, a Script include can be called from both the client and server side regardless if the client callable checkbox is ticked.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

https://www.servicenow.com/community/developer-advocate-blog/client-callable-and-server-callable-scr...

View solution in original post

4 REPLIES 4

Robbie
Kilo Patron
Kilo Patron

Hi @MBarrott,

 

It sure can.

Rather than reinvent the wheel - check this great article written @Earl Duque (Kudos) who provides perfect examples of the 'how' this is achieved and the syntax - see below link.

 

But to answer your question directly - Yes, a Script include can be called from both the client and server side regardless if the client callable checkbox is ticked.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

 

https://www.servicenow.com/community/developer-advocate-blog/client-callable-and-server-callable-scr...

Thanks @Robbie - I will give this a try and see if it works out. 

 

Looks like the example @Earl Duque shows is calling a singular function from both client and server. In my situation it would likely be A function for server, B function for client (at least right now). Thinking the bold part he illustrates won't necessarily be applicable but it's certainly good to know. 

Hi @MBarrott,

 

This is absolutely possible. A Script Include is not restricted by the number of functions contained within it. It is in fact very common to see multiple functions including sometimes 'private' functions which have limited access - think of 'Util' "classes" for example.

 

Have a play and let me know if you have any issues, but answering your question - a Script Include is absolutely accessible via both the client and server side when the 'client callable' checkbox is ticked.

 

To help others (or for me to help you more directly), please mark this response correct by clicking on Accept as Solution and/or Kudos.



Thanks, Robbie

Brad Bowman
Kilo Patron
Kilo Patron

Yes!  While the bit that needs to be added (but probably won't be automatically if a script already exists when clicking the Client callable box) is required when calling the Script Include from a Client Script using GlideAjax, it doesn't hurt anything to have this bit when it's not needed.  So a "Client callable" Script Include can be called from a Business Rule, UI Action condition, reference qualifier, ... just fine.  You can even use the same function for both client and server calls like this:

var ritmUtils = Class.create();
ritmUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getUsers: function(sysid){
		var usrsysid = this.getParameter('sysparm_user_sys_id') || sysid;
                ...

Where 'sysid' is a value passed in from a reference qualifier or server script call as an argument, and sysparm_user_sys_id is passed in from a GlideAjax call in a Client Script.  If the client parameter is empty, the script variable is set to the server call argument.