- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 05:56 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 06:52 AM
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]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 06:52 AM
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]);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 10:38 AM
Hi @Brad ,
Thanks for your input. I have tried the same approach however, it is not working in updating the dropdown.
For some reason JSON.parse for answer variable is not working in client script. Please suggest.
Best Regards,
Subhashree
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 11:56 AM
Subhashree Sub1,
What do you get back with answer[key] in the callback? I'm not as familiar with that syntax but I wonder if it should be like this. It also looks to me like you're only grabbing one piece of the Key/Value pair.
for (key in answer) {
g_form.addOption('country', key.name, key.value);
}
Have you tried it like this syntax instead?
for (i=0; i<answer.length; i++) {
g_form.addOption('country', answer[i].name, answer[i].value);
}
What does the string look like when it gets back to the client?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2022 12:45 PM
Hmm. To check the scripts I created a system property with your value (minus the spaces/new lines but I don't think that should matter). I put my Catalog Client Script onLoad so the select box variable is populated when the form loads, and this is working fine for me. Are you getting an error, or just nothing in the drop-down? If you alert on answer or answer[0] what do you get? If you add a gs.addInfoMessage to the Script Include, is the property value successfully shown?