- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 05:52 AM
Hi all,
We have a custom table that contains a service, category and a subcategory. We also have a variable set that is connected to several items. On the variable set there is a field called internal service. When a change happens on this field a catalog client script is run. The catalog client script is doing a glideajax lookup using a script include that goes towards the custom table, trying to lookup all categories for the internal service the user just selected. I have gotten the script working, I am getting every value I need, but when I am trying to add them to the variable in the variable set, only one variable gets stored. It is looping trough all the categories (I checked from logs), but it is not getting added.
Script include:
Catalog Client Script:
Example:
I am choosing my internal service (Finance), then I am supposed to get several categories, but I am only getting the last one one.
Adding the field as well, just for reference:
Any suggestions?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 06:09 AM
Updated Script include (untested)
var GSS_GetCategory = Class.create();
GSS_GetCategory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCategories: function() {
var Categories= [];
//Gliderecord lookup
var gr = new GlideRecord("u_gss_category_mapping");
gr.addQuery('u_internal_service', 'IN' , this.getParameter('sysparm_internal_service_name'));
gr.orderBy('u_internal_service');
gr.query();
while(gr.next()) {
var cObj = {};
cObj.internal_service = gr.getValue('u_internal_service');
cObj.gss_category = gr.getValue('u_gss_category');
Categories.push(cObj);
gs.log("PPP_Script include__1111_ gr.GetValue(u_gss_category) = " + gr.getValue('u_gss_category'));
gs.log("PPP_Script include__2222_ gr.GetValue(u_internal_service) = " + gr.getValue('u_internal_service'));
}
gs.log("PPP_Script include__3333_ This is being returned to the catalog client script = " + Categories);
return JSON.stringify(Categories);
},
_privateFunction: function() { // this function is not client callable
}
});
Updated client script (untested)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
//g_form.clearOptions('category');
//g_form.addOption('category', '0', '-- None --');
return;
}
alert("Running Client Script");
//g_form.clearOptions('category');
//g_form.addOption('category', '0', '-- None --');
var ga = new GlideAjax('GSS_GetCategory');
ga.addParam('sysparm_name','getCategories');
ga.addParam('sysparm_internal_service_name',g_form.getValue('internal_service'));
ga.getXML(errorTypeParse);
function errorTypeParse(response) {
answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer+"testIDandCategory");
var Categories = JSON.decode(answer);
var i = 0;
while(i < Categories.length)
{
g_form.addOption('category',Categories[i].internal_service, Categories[i].gss_cateogy);
i++;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 05:57 AM
Petter,
can you post the code instead of screenshot. That way I could make changes to your code easily.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 06:03 AM
Script include:
- var GSS_GetCategory = Class.create();
- GSS_GetCategory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
- getCategories: function() {
- var Categories="";
- //Gliderecord lookup
- var gr = new GlideRecord("u_gss_category_mapping");
- gr.addQuery('u_internal_service', 'IN' , this.getParameter('sysparm_internal_service_name'));
- gr.orderBy('u_internal_service');
- gr.query();
- while(gr.next()) {
- Categories+=gr.getValue('u_internal_service')+"|"+gr.getValue('u_gss_category')+",";
- gs.log("PPP_Script include__1111_ gr.GetValue(u_gss_category) = " + gr.getValue('u_gss_category'));
- gs.log("PPP_Script include__2222_ gr.GetValue(u_internal_service) = " + gr.getValue('u_internal_service'));
- }
- gs.log("PPP_Script include__3333_ This is being returned to the catalog client script = " + Categories);
- return Categories;
- },
- _privateFunction: function() { // this function is not client callable
- }
- });
Catalog client script:
- function onChange(control, oldValue, newValue, isLoading, isTemplate) {
- if (isLoading || newValue == '') {
- //g_form.clearOptions('category');
- //g_form.addOption('category', '0', '-- None --');
- return;
- }
- alert("Running Client Script");
- //g_form.clearOptions('category');
- //g_form.addOption('category', '0', '-- None --');
- var ga = new GlideAjax('GSS_GetCategory');
- ga.addParam('sysparm_name','getCategories');
- ga.addParam('sysparm_internal_service_name',g_form.getValue('internal_service'));
- ga.getXML(errorTypeParse);
- function errorTypeParse(response) {
- answer = response.responseXML.documentElement.getAttribute("answer");
- alert(answer+"testIDandCategory");
- var splitStr = answer.split(",");
- var tmpStr = splitStr.slice(0,-1);
- alert("tmpStr + split = " + tmpStr);
- var i = 0;
- while(i < splitStr.length)
- {
- var splitValues = tmpStr[i].split('|');
- g_form.addOption('category',splitValues[0],splitValues[1]);
- i++;
- }
- }
- }

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 06:09 AM
Updated Script include (untested)
var GSS_GetCategory = Class.create();
GSS_GetCategory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCategories: function() {
var Categories= [];
//Gliderecord lookup
var gr = new GlideRecord("u_gss_category_mapping");
gr.addQuery('u_internal_service', 'IN' , this.getParameter('sysparm_internal_service_name'));
gr.orderBy('u_internal_service');
gr.query();
while(gr.next()) {
var cObj = {};
cObj.internal_service = gr.getValue('u_internal_service');
cObj.gss_category = gr.getValue('u_gss_category');
Categories.push(cObj);
gs.log("PPP_Script include__1111_ gr.GetValue(u_gss_category) = " + gr.getValue('u_gss_category'));
gs.log("PPP_Script include__2222_ gr.GetValue(u_internal_service) = " + gr.getValue('u_internal_service'));
}
gs.log("PPP_Script include__3333_ This is being returned to the catalog client script = " + Categories);
return JSON.stringify(Categories);
},
_privateFunction: function() { // this function is not client callable
}
});
Updated client script (untested)
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
//g_form.clearOptions('category');
//g_form.addOption('category', '0', '-- None --');
return;
}
alert("Running Client Script");
//g_form.clearOptions('category');
//g_form.addOption('category', '0', '-- None --');
var ga = new GlideAjax('GSS_GetCategory');
ga.addParam('sysparm_name','getCategories');
ga.addParam('sysparm_internal_service_name',g_form.getValue('internal_service'));
ga.getXML(errorTypeParse);
function errorTypeParse(response) {
answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer+"testIDandCategory");
var Categories = JSON.decode(answer);
var i = 0;
while(i < Categories.length)
{
g_form.addOption('category',Categories[i].internal_service, Categories[i].gss_cateogy);
i++;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-05-2016 06:13 AM
A minor correction, in the client script
g_form.addOption('category',Categories[i].internal_service, Categories[i].gss_category);