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

So instead of var globalVar = ''; you just need to use globalVar = '';


Please mark this response as correct or helpful if it assisted you with your question.

Alok Das
Tera Guru

Hi John,

Global variable can be defined in client script and called using g_scratchpad object but it does not work for service catalog form.

Please find the below snipped and related HI link below:

find_real_file.png

https://hi.service-now.com/kb_view.do?sysparm_article=KB0749175

Kindly mark my answer as Correct and Helpful based on the impact.

Regards,

Alok

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

BINGO x 100! Thanks you so much!

This works! Amazing. Thank you so much! My question is I do not understand why we have to declare the global variable onLoad, assign a new value onChange 1, then use it at onChange 2. I tried to declare the global variable at onLoad, then use it at another onChange but it does not work though.