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

richard-service
ServiceNow Employee
ServiceNow Employee

Hey Conan,



You are only sending sys ID's back.   The client script has no context of the records themselves since it is executed at the browser.   Here is what you will want to do.



On your script include:


First, you are better off with populating an array and then joining it rather than trying to manually muck with commas.   But I would create an array of objects to send back.



arrOptions = [];


while(gp.next()) {  


  arrOptions.push({


        id: gp.getUniqueValue(),


        label: gp.getDisplayValue('label_field')


  });


}


return arrOptions.join(',');



On your script include:



Make sure the 'label_field' is actually the name of the field that has the name of the record rather than that placeholder.   Once you have that, you'll be sending over both the id and the label.   You can then use that to populate the options.   Since you're passing back an array of objects you will need to parse the JSON object.



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


...


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


Thanks for that nudge, I have very little experience with arrays and am not sure I have your code in the right place. Can you look it over for me?



When i try it I get no options in the drop down.



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 arrOption = JSON.parse(response.responseXML.documentElement.getAttribute('answer'));


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


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


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


  }


  }


}



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();


  arrOptions = [];


  while(gp.next()) {


  arrOptions.push({


  id: gp.getUniqueValue(),


  label: gp.getDisplayValue('u_item_name')


  });


  }


  return arrOptions.join(',');


  }


  catch(e) {


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


  }


  },


  type: 'ergoChoiceValues'


});


Hi Conan,



Can we try like,



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 strOption = answer.evalJSON();


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


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


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


}  


}  


}  






Script Include:


var ergoChoiceValues = Class.create();  


ergoChoiceValues.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


getChoiceValues: function() {  


try {


var object = {};


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()) {  


object.id = gp.getUniqueValue();


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


}


var json = new JSON();


return json.encode(object);


}  


catch(e) {  


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


}  


},  


type: 'ergoChoiceValues'  


});  



Hey Shishir Srivastava, thanks for the reply.   I put in your script include and your client script, but added a few alerts to see how far it was getting.   Here is the current 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) {


  alert("CDL - UpdateChoiceValues started");


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


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


var strOption = answer.evalJSON();


  alert("CDL - strOption declared: "+ strOption);


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


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


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


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


}      


}      


}



I get the "CDL - UpdateChoiceValues started" and I get "CDL - Answer declared: " with 1 value/pair label.   However, I don't get the "CDL - strOption declared: " alert, implying that it fails there.   Also, shouldn't the answer variable have had multiple value/label sets in it?   My, admittedly uneducated, glance at your script include doesn't seem to tell it to append to the json, but looks like each iteration through the glide lookup would overwrite the previous?



Still trying to get this operational.