Populate Lookup Select Box using GlideAjax question

conanlloyd
Giga Guru

I have a catalog variable named ergo_keyboard_choice that looks up values in the u_ergonomic_equipment table nut the selection criteria needs to be able to change based on the answer in the assessment field.   After fumbling around trying to use a reference qualifier to conditionally populate a lookup select box on my catalog item, (history here), I gave up and took a different tack.

Using this great post by parvinder as a springboard, I was able to get a GlideAjax about 90% working.   The good news is that I see the right number of options populating the lookup select box.   The bad news is that I am only seeing sysIDs in the box instead of names.   For the life of me I can't figure out how to get the item name(u_item_name) in there with the sys_id.   I see that the script is setting both the value and the label of each option as the sys_id in line 28 of the client script below, but i can't figure out how to get the u_item_name value in there for the label.

Help?

Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {

  if (isLoading || newValue == '') {

  g_form.clearOptions('ergo_keyboard_choice');

  return;

  }

  else {

  if (g_form.getValue('assessment')!='none' && g_form.getValue('assessment')!=''){

  //remove all items from drop down to start

  g_form.clearOptions('ergo_keyboard_choice');

  var ga = new GlideAjax('ergoChoiceValues');

  ga.addParam('sysparm_name','getChoiceValues');

  ga.addParam('sysparm_ergo_req', 'true');

  ga.addParam('sysparm_ergo_type', 'keyboard');

  ga.addParam('sysparm_ergo_location', 'tarrytown');

  ga.getXML(UpdateChoiceValues);

  }

  }

  //process response

  function UpdateChoiceValues(response) {

  var strOption = '';

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

  if(answer != null) {

  strOption = answer.split(',');

  }

  g_form.addOption('ergo_keyboard_choice', '', '-- None --');

  for(var i=0;i<strOption.length;i++) {

  g_form.addOption('ergo_keyboard_choice', strOption[i], strOption[i]);

  }

  }

}

Script Include:

var ergoChoiceValues = Class.create();

ergoChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {

  getChoiceValues: function() {

  try {

  var strOptions = '';

  var gp = new GlideRecord('u_ergonomic_equipment');

  gp.addQuery('u_needs_assessment', this.getParameter('sysparm_ergo_req'));

  gp.addQuery('u_item_type', this.getParameter('sysparm_ergo_type'));

  gp.addQuery('u_item_location', this.getParameter('sysparm_ergo_location'));

  gp.addQuery('u_active', true);

  gp.query();

  while(gp.next()) {

  strOptions += ',' + gp.sys_id;

  }

  if(strOptions.startsWith(',')) {

  strOptions = strOptions.substring(1);

  }

  return strOptions;

  }

  catch(e) {

  gs.log("CDL - E: " + e.message);

  }

  },

  type: 'ergoChoiceValues'

});

Message was edited by: Conan Lloyd :: Fixed url.

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

can we replace answer = answer.evalJSON(); with var myObj = JSON.parse(answer); like,



  var myObj = JSON.parse(answer);


  alert("CDL - answer evaluated");  


  g_form.addOption('ergo_keyboard_choice', '', '-- None --');  


  for(var i=0; i<myObj.length; i++) {  


  g_form.addOption('ergo_keyboard_choice', myObj[i].id, myObj[i].label);  


  alert("CDL - Option added" + myObj[i].id + " "+ myObj[i].label);  


View solution in original post

8 REPLIES 8

Hi Conan,



Please check if this helps, we need to pass the array of value.



Client Script:


function onChange(control, oldValue, newValue, isLoading) {      


if (isLoading || newValue == '') {      


g_form.clearOptions('ergo_keyboard_choice');      


return;      


}      


else {      


if (g_form.getValue('assessment')!='none' && g_form.getValue('assessment')!=''){      


//remove all items from drop down to start      


g_form.clearOptions('ergo_keyboard_choice');


var ga = new GlideAjax('ergoChoiceValues');      


ga.addParam('sysparm_name','getChoiceValues');      


ga.addParam('sysparm_ergo_req', 'true');      


ga.addParam('sysparm_ergo_type', 'keyboard');      


ga.addParam('sysparm_ergo_location', 'tarrytown');      


ga.getXML(UpdateChoiceValues);      


}      


}      


//process response      


function UpdateChoiceValues(response) {      


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


answer = answer.evalJSON();    


g_form.addOption('ergo_keyboard_choice', '', '-- None --');      


for(var i=0; i<answer.length; i++) {      


g_form.addOption('ergo_keyboard_choice', answer[i].id, answer[i].label);      


}      


}      


}      




Script Include:


var ergoChoiceValues = Class.create();      


ergoChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {      


getChoiceValues: function() {      


try {  


var array = [];


var gp = new GlideRecord('u_ergonomic_equipment');      


gp.addQuery('u_needs_assessment', this.getParameter('sysparm_ergo_req'));      


gp.addQuery('u_item_type', this.getParameter('sysparm_ergo_type'));      


gp.addQuery('u_item_location', this.getParameter('sysparm_ergo_location'));      


gp.addQuery('u_active', true);      


gp.query();        


while(gp.next()) {  


var object = {};  


object.id = gp.getUniqueValue();  


object.label = gp.getDisplayValue('u_item_name');


array.push(object);


}    


var json = new JSON();  


var data = json.encode(array);


return data;  


}      


catch(e) {      


gs.log("CDL - E: " + e.message);      


}      


},      


type: 'ergoChoiceValues'      


});



Reference: https://fruitionpartners.eu/blog/2015/11/17/glideajax-return-multiple-values-using-json/


I really appreciate you responding again, we are making definite progress as my second alert now has multiple value/label pairs.



BTW, "answer = answer.evalJSON();" no longer works with the portal but I found another post stating that "answer = JSON.parse(answer);" works and it did.



I even got the script to add both the none value and the first from the array.



However, I never got the alert on line 29 below and only get the one option added.



function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  g_form.clearOptions('ergo_keyboard_choice');


  return;


  }


  else {


  if (g_form.getValue('assessment')!='none' && g_form.getValue('assessment')!=''){


  //remove all items from drop down to start


  g_form.clearOptions('ergo_keyboard_choice');


  var ga = new GlideAjax('ergoChoiceValues');


  ga.addParam('sysparm_name','getChoiceValues');


  ga.addParam('sysparm_ergo_req', 'true');


  ga.addParam('sysparm_ergo_type', 'keyboard');


  ga.addParam('sysparm_ergo_location', 'tarrytown');


  ga.getXML(UpdateChoiceValues);


  }


  }


  //process response


  function UpdateChoiceValues(response) {


  alert("CDL - UpdateChoiceValues started");


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


  alert("CDL - Answer declared: "+ answer);


  // answer = answer.evalJSON();


answer = JSON.parse(answer);  


alert("CDL - answer evaluated");


  g_form.addOption('ergo_keyboard_choice', '', '-- None --');


  for(var i=0; i<answer.length; i++) {


  g_form.addOption('ergo_keyboard_choice', answer[i].id, answer[i].label);


  alert("CDL - Option added" + arrOption[i].id + " "+arrOption[i].label);


  }


  }


}


Shishir Srivast
Mega Sage

can we replace answer = answer.evalJSON(); with var myObj = JSON.parse(answer); like,



  var myObj = JSON.parse(answer);


  alert("CDL - answer evaluated");  


  g_form.addOption('ergo_keyboard_choice', '', '-- None --');  


  for(var i=0; i<myObj.length; i++) {  


  g_form.addOption('ergo_keyboard_choice', myObj[i].id, myObj[i].label);  


  alert("CDL - Option added" + myObj[i].id + " "+ myObj[i].label);  


That was the last piece of the puzzle!   You are a rockstar and I can't thank you enough.



You should update your previous reply with the whole script include and the whole client script so the complete correct scripts are on the post I marked as the correct answer.   I have re-pasted both here so you can copy/paste



Working Client Script:


function onChange(control, oldValue, newValue, isLoading) {


  if (isLoading || newValue == '') {


  g_form.clearOptions('ergo_keyboard_choice');


  return;


  }


  else {


  if (g_form.getValue('assessment')!='none' && g_form.getValue('assessment')!=''){


  //remove all items from drop down to start


  g_form.clearOptions('ergo_keyboard_choice');


  var ga = new GlideAjax('ergoChoiceValues');


  ga.addParam('sysparm_name','getChoiceValues');


  ga.addParam('sysparm_ergo_req', 'true');


  ga.addParam('sysparm_ergo_type', 'keyboard');


  ga.addParam('sysparm_ergo_location', 'tarrytown');


  ga.getXML(UpdateChoiceValues);


  }


  }


  //process response


  function UpdateChoiceValues(response) {


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


  var myObj = JSON.parse(answer);


  g_form.addOption('ergo_keyboard_choice', '', '-- None --');


  for(var i=0; i<myObj.length; i++) {


  g_form.addOption('ergo_keyboard_choice', myObj[i].id, myObj[i].label);


  }


  }


}






Working Script Include:


var ergoChoiceValues = Class.create();


ergoChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getChoiceValues: function() {


  try {


  var array = [];


  var gp = new GlideRecord('u_ergonomic_equipment');


  gp.addQuery('u_needs_assessment', this.getParameter('sysparm_ergo_req'));


  gp.addQuery('u_item_type', this.getParameter('sysparm_ergo_type'));


  gp.addQuery('u_item_location', this.getParameter('sysparm_ergo_location'));


  gp.addQuery('u_active', true);


  gp.query();


  while(gp.next()) {


  var object = {};


  object.id = gp.getUniqueValue();


  object.label = gp.getDisplayValue('u_item_name');


  array.push(object);


  }


  var json = new JSON();


  var data = json.encode(array);


  return data;


  }


  catch(e) {


  gs.log("CDL - E: " + e.message);


  }


  },


  type: 'ergoChoiceValues'


  });