Using shared catalog client scripts

John Spencer
Tera Contributor

Hello All,

 

Is there a 'reliable' mechanism to allow 'shared' code between / among catalog client scripts in the same catalog item?

 

That is, a client catalog script would load with the intent of being able to be called by another client catalog script in the same catalog item.  Presently using Yokohama (patch 10).

 

Thanks in advance,

John Spencer

5 REPLIES 5

balu_one
Tera Contributor

Hi John,

 

I think the approach of using the shared function in the onLoad script works fine. But it is not a documented approach as such, and I have seen it in this community post. The credit goes to them.

 

Steps:

  1. Create an onLoad script which stores the reusable shared code. Say something like this
function onLoad() {
}

/* Shared functions */
sayHello = function (msg) {
	alert('Hello ' + msg);
}​
  • Create an onChange script that calls the Shared functions
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   sayHello('John');
}​

This should work in Native UI and Service Portal. I'm not sure about Workspaces and Mobile.

And also note, the shared functions must be written in the following format to make it work in Service Portal.

function_name = function () {
    // Do something
}

 

I agree with Jeffrey Bell. The main reason we’d need this is when multiple onChange scripts share the same logic. However, I’ve also encountered forms that require certain validations before even initiating an Ajax call. It can be quite a tricky situation, I must say.

 

Sometimes I wonder why wouldn't ServiceNow just implement

  1. A mechanism to have form only reusable object (similar to g_scratchpad) within Catalog form.
  2. Allow variables that exist only on the Catalog Form, without being submitted or included in the Requested Item

They may seem so minor, but they significantly enhance the overall strength of the Form’s client-side capabilities.

 

Thanks
Balu