How to update the options of a variable from a key-value pair system property?

Subhashree Sub1
Tera Contributor

Hi All,

 

I have a requirement where I have to add options to a variable (Select box type) by pulling values from a key-value pair system property.

The property has data in below format:

{

"IN":"India",

"US":"United States",

"UK":"United Kingdom"

}

The type of property is: String

I have created a a script include where I am calling this property and parsing it. Throgh GlideAjax in the onChange client script, I need to take the response and show only values (like: India,United States,United Kingdom etc) in the options of a variable.

The parsing logic which I have currently is not adding the options of the variable. Please review and suggest your approach. The logic works fine in the Background script where as in the client script it is not working.

 

Script include logic:

var countryProp = gs.getProperty('countryProperty');

var parsedCountry = new JSONParser().parse(countryProp);

return parsedCountry;

 

onChange Client script logic:

var countryAjax = new GlideAjax('Country Script Include');

countryAjax.addParam('sysparm_name','getCountryList');

countryAjax.getXML(getCountryCallBack);

function getCountryCallBack(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

for(var key in answer ){

g_form.addOption('country', answer[key], answer[key]);

}

 

Please let me know if you have any suggestion.

 

Thank you!

Best Regards,

Subhashree

1 ACCEPTED SOLUTION

Brad Bowman
Kilo Patron
Kilo Patron

Only string values can be passed between server and client, and vice-versa, so your Script Include can simply be:

var CountryScriptInclude = Class.create();
CountryScriptInclude.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	getCountryList : function(){
		return gs.getProperty('countryProperty');
	},
	
    type: 'CountryScriptInclude'
});

and then to parse the JSON in the Client Script would look more like this:

var countryAjax = new GlideAjax('CountryScriptInclude');
countryAjax.addParam('sysparm_name', 'getCountryList');
countryAjax.getXML(getCountryCallBack);

function getCountryCallBack(response) {
    var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
    for (var key in answer) {
        g_form.addOption('country', answer[key], answer[key]);
    }
}

View solution in original post

7 REPLIES 7

Hi @Brad Bowman  ,

 

Here I am trying to add options through onChange client script. Whenever I write JSON.parse to parse the answer, the alert just doesn't work in this case but if I remove it (JSON.parse) the answer value holds the property value(i.e., key-value pair of countries).

In onChange client script with the given client script code, my dropdown doesn't list out all the options as the parsing is not happening for answer.

The script include correctly returns the property value. Its just at the onChange client side the parsing is not working.

Please suggest.

 

Thanks for all your inputs!

Best Regards,

Subhashree

I changed my system property to the way you have it, even with all of the spaces/carriage returns it still seems to work OK.  I changed my script to onChange with an alert - also works grand in the native UI and Service Portal

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

   var countryAjax = new GlideAjax('CountryScriptInclude');
    countryAjax.addParam('sysparm_name', 'getCountryList');
    countryAjax.getXML(getCountryCallBack);

    function getCountryCallBack(response) {
        var answer = JSON.parse(response.responseXML.documentElement.getAttribute("answer"));
		alert(answer["IN"])
        for (var key in answer) {
            g_form.addOption('country', answer[key], answer[key]);
        }
    }
}

If this Catalog Client Script is not in the Global scope/application, try this modification:

var answer = response.responseXML.documentElement.getAttribute("answer");
answer = new global.JSON.parse(answer);

 

Hi @Brad Bowman  , The issue was with the extra spaces in the country list property. After clearing all the extra spaces, the parsing worked fine. Thank you for your suggestion on this. Accepting your solution! 🙂