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

Hi Chuck,

I have tried to follow your method for a slimier requirements but values are not returned to the Record Producer. And there are no log out puts from the Script include. The client script is called because Case Subtype values are cleared.  

I have choice list on which is dependant on another.

Case Subtype is dependant on Case Type. 

Script include:

var getCaseSubType = Class.create();
getCaseSubType.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
 
 getCaseSubType : function() {
 gs.log("Script include getCaseSubType Called!!");
 var caseType = this.getParameter('sysparm_caseType');
 var subType = [];
 
 var gr = new GlideRecord("sys_choice");
 gr.addQuery("table", "sn_customerservice_case");
 gr.addQuery("element", "u_case_subtype");
 gr.addQuery("inactive", false);
 gr.addQuery("dependent_value", caseType);
 gr.orderBy('value');
 gr.query();
 while (gr.next()) {
 var obj = {};
 obj.value = gr.value;
 obj.label = gr.label;
 subType.push(obj);
 
 gs.log("obj.value " + gr.value);
 gs.log("obj.label " + gr.label);
 }
 gs.log("List to be returned " + subType);
 return JSON.stringify(subType);
 },
 _privateFunction: function() { // this function is not client callable
 }
 //type: 'getCaseSubType'
 });

Client Script

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || (oldValue == newValue)) {
 return;
 }
 //Clear all options
 g_form.clearOptions('u_case_subtype');
 
 //call Script Include: getCaseSubType and function: getCaseSubType
 var ga = new GlideAjax('getCaseSubType');
 ga.addParam('sysparm_name', 'getCaseSubType');
 ga.addParam('sysparm_caseType', newValue);
 ga.getXML(subTypeParse);
 
 function subTypeParse(response) {
 answer = response.responseXML.documentElement.getAttribute("answer");

 alert(answer + " Subtype");

 var subType = JSON.decode(answer);
 
 for (var i = 0; i < answer.length; i++) {
 g_form.addOption('u_case_subtype', subType[i].value, subType[i].label);
 }
 }
}

If there are no log outputs, then check the console log in your browser to see if there is anything odd on the client side.

Also, if you do get the script include to run, change these:

 

 

obj.value = gr.value;
obj.label = gr.label;

to

obj.value = gr.getValue('value');
obj.label = gr.getValue('label');

Shoaib Sheth
Kilo Explorer

Thank You for your help Chuck. 

I updated the  Script Include as per your comment, however in the console log there are no errors. 

log out put of the answer is null

This is an scooped application, I'm I missing something? 

I have included the API name in the Ajax call 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || (oldValue == newValue)) {
		return;
	}
	//Clear all options
	g_form.clearOptions('u_case_subtype');
	
	var ga = new GlideAjax('sn_customerservice.getCaseSubType');
	ga.addParam('sysparm_name', 'getCaseSubType');
	ga.addParam('sysparm_caseType', newValue);
	ga.getXML(subTypeParse);
	
	function subTypeParse(response) {
		//console.log(response);
		var answer = response.responseXML.documentElement.getAttribute("answer");
		console.log(answer);
		
		answer = JSON.parse(answer);
		//for (var i = 0; i < answer.length; i++) {
			//g_form.addOption('u_case_subtype', answer[i].value, answer[i].label);
			//}
		}
	}

 

 

 

Did you write sn_customerservice or is this OOB? If it is one of ours, you'll need to prefix the scope for that.

 

Ex:

var ga = new GlideAjax('scopename.sn_customer_service');

// Leave off the function/method. That is only used in the sysparm_name