GlideAjax in Catalog Client Script

petterbj_rgmo
Tera Contributor

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:

find_real_file.png

Catalog Client Script:

find_real_file.png

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.

find_real_file.png

Adding the field as well, just for reference:

find_real_file.png

Any suggestions?

1 ACCEPTED SOLUTION

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++;


  }


  }


}


View solution in original post

29 REPLIES 29

Also fixed. Thanks.



We need a Google Docs/live update collaborative feature for things like this.


I implemented the script, but it is still not working. I will add some logs, and add them here!


Agree


Script include (Updated):


  1. var GSS_GetCategory = Class.create();
  2. GSS_GetCategory.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  3.   getCategories: function() {
  4.   var Categories= [];
  5.   //Gliderecord lookup
  6.   var gr = new GlideRecord("u_gss_category_mapping");
  7.   gr.addQuery('u_internal_service', 'IN' , this.getParameter('sysparm_internal_service_name'));
  8.   gr.orderBy('u_internal_service');
  9.   gr.query();
  10.   while(gr.next()) {
  11.   var cObj = {};
  12.   cObj.internal_service = gr.getValue('u_internal_service');
  13.   cObj.gss_category = gr.getValue('u_gss_category');
  14.   Categories.push(cObj);
  15.   gs.log("PPP_Script include__1111_   gr.GetValue(u_gss_category) = " + gr.getValue('u_gss_category'));
  16.   gs.log("PPP_Script include__2222_   gr.GetValue(u_internal_service) = " + gr.getValue('u_internal_service'));
  17.   }
  18.   gs.log("PPP_Script include__3333_   This is being returned to the catalog client script = " + Categories);
  19.   return JSON.stringify(Categories);
  20.   },
  21.   _privateFunction: function() { // this function is not client callable
  22. }
  23. });


Client script (updated):



  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  2.   if (isLoading || newValue == '') {
  3.   //g_form.clearOptions('category');
  4.   //g_form.addOption('category', '0', '-- None --');
  5.   return;
  6.   }
  7.   alert("Running Client Script");
  8.   //g_form.clearOptions('category');
  9.   //g_form.addOption('category', '0', '-- None --');
  10.   var ga = new GlideAjax('GSS_GetCategory');
  11.   ga.addParam('sysparm_name','getCategories');
  12.   ga.addParam('sysparm_internal_service_name',g_form.getValue('internal_service'));
  13.   ga.getXML(errorTypeParse);
  14.   function errorTypeParse(response) {
  15.   answer = response.responseXML.documentElement.getAttribute("answer");
  16.   alert("Answer is: " + answer);
  17.   var Categories = JSON.decode(answer);
  18.   var i = 0;
  19.   alert("Categories: " + Categories);
  20.   while(i < Categories.length)
  21.   {
  22.   g_form.addOption('category',Categories[i].internal_service, Categories[i].gss_cateogry);
  23.   alert("Inside while loop, print int_s and cat: " + Categories[i].internal_service + " " + Categories[i].gss_category);
  24.   i++;
  25.   }
  26.   }
  27. }

Logs:



find_real_file.png



It is not running the alert which is inside the while loop... So I am guessing it is not going in the while loop for some reason


In your client script, add this alert


  1. function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  2.   if (isLoading || newValue == '') {
  3.   //g_form.clearOptions('category');
  4.   //g_form.addOption('category', '0', '-- None --');
  5.   return;
  6.   }
  7.   alert("Running Client Script");
  8.   //g_form.clearOptions('category');
  9.   //g_form.addOption('category', '0', '-- None --');
  10.   var ga = new GlideAjax('GSS_GetCategory');
  11.   ga.addParam('sysparm_name','getCategories');
  12.   ga.addParam('sysparm_internal_service_name',g_form.getValue('internal_service'));
  13.   ga.getXML(errorTypeParse);
  14.   function errorTypeParse(response) {
  15.   answer = response.responseXML.documentElement.getAttribute("answer");
  16.   alert("Answer is: " + answer);
  17.   var Categories = JSON.decode(answer);
  18. alert(Categories.length);
  19.   var i = 0;
  20.   alert("Categories: " + Categories);
  21.   while(i < Categories.length)
  22.   {
  23.   g_form.addOption('category',Categories[i].internal_service, Categories[i].gss_cateogry);
  24.   alert("Inside while loop, print int_s and cat: " + Categories[i].internal_service + " " + Categories[i].gss_category);
  25.   i++;
  26.   }
  27.   }
  28. }