Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to access global variable in catalog client scripts?

John_123_Snow
Mega Expert

I'm trying to create a global variable in a Catalog Client Script, available for all other client scripts to access:

var globalVar = '';

function onLoad() {

	var ajax = new GlideAjax('TestScriptInclude');
	ajax.addParam('sysparm_name', 'returnProperty');
	ajax.getXMLAnswer(function(answer) {
		globalVar = answer;
	});

}

This onLoad client script calls to the server and gets an array of values that I need for other client scripts to use with onChange and onSubmit.

How do I set this up correctly and access it in my other catalog client scripts?

1 ACCEPTED SOLUTION

Hitoshi Ozawa
Giga Sage
Giga Sage

Be sure to uncheck "Isolate script" on all scripts using global variable.

  1. Declare global variable in onLoad.
    find_real_file.png
  2. Set value onChange script.
    find_real_file.png
  3. Can use it on other onChange script
    find_real_file.png

Execution output:

find_real_file.png

View solution in original post

10 REPLIES 10

Vedo
Tera Contributor

Hi, I have tried as described above but in the onChange function the global variable is not defined. I have follwoing error message: 

ReferenceError: globalVarTest is not defined

 

Isolate script on all script is unchecked