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

SanjivMeher
Kilo Patron
Kilo Patron

I don think that is possible at all. What is your use case


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

The use case is simple...better readibility and maintainability of the code to keep most of the logic in Catalog Client Script, keep it in one place. Do not want to call the same script include in glide ajax, onChange, onSubmit, onLoad to just get this property.

Then you need to create a variable and store it in the variable of catalog item. I dont think there is a concept of global variable in catalog client script.


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

Yes there is, I've tried it myself.

 

https://community.servicenow.com/community?id=community_question&sys_id=1caa472ddb5cdbc01dcaf3231f961956

 

I could access this in other Catalog Client Scripts in that Item.