How can I set a service catalog variable's reference qualifier with a service catalog script?

Stephen W_
Giga Guru

I have a variable set for use with Service Catalog items.

For this requirement, I cannot hard-code most of the the field attributes, they are dynamically defined.

 

As such, I need an onLoad() script that runs and displays/hides each of these fields and sets up their reference qualifiers..     To make this work, I need to be able to set the reference qualifier for each variable from this script.

 

How can I access this attribute?

 

Thanks,

-Stephen

1 ACCEPTED SOLUTION

Stephen W_
Giga Guru

The solution for this question was a combination of Srikanth's suggestion and something I found elsewhere.


(apologies if there are any mistakes here, I ended up going another route and don't have this exact code anymore)




Catalog Client Script:


OnLoad(){


var scVarElement = {};


var varMap = gel('variable_map');


var items = varMap.getElementsByTagName("item");



//This is the only way I found to get the sys_id for the item_option_new record.


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


  var item = items.item(i);


  var name = item.getAttribute('qname');


  var id = item.id;


    scVarElement = {name:name,id:id,refqualifier:"whateverencodedqueryyouwant"};


}



//Now call an include script method with AJAX


var ga = new GlideAjax('ScItemIntfTypes');


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


  ga.addParam('sysparm_sc_options', scVarElement);


  ga.getXML();


}



Script Include:


setQuestions: function() {


  var grScOptions = new GlideRecord('item_option_new');



  var scQuestion = this.getParameter('sysparm_sc_options');


  if (grScOptions.get(scQuestion.id)){


      grScOptions.reference_qual = scQuestion.refqualifier;


      grScOptions.update();


  }


}




**Note that I ended up switching my 'Lookup Multiple Choice' fields to 'Select Box' fields since setting the reference qualifier would unnecessarily burden the server.   The choice lists are now populated via the client script.


View solution in original post

8 REPLIES 8

srikanthvk
Giga Expert

Initialize a GlideAjax and call the script include from your client script. Now do a GlideRecord in your script include on 'item_option_new' table to access the reference_qual and update the reference qualifier accordingly.


Srikanth,


that was helpful thanks.   I was looking at the wrong table.



'item_option_new' got me much closer.   However, there are multiple records in 'item_option_new' for each variable, and I'm not sure how to get the sys_id for the 'item_option_new' record to pass to the script_include..



console.log(g_form.getValue('scAttrib1').toString()); //this returns the sys_id of the current value of the variable


console.log(g_form.getReference('scAttrib1').sys_id); //this returns 'undefined'



The information I'm looking for is available in div id "variable_map".. but I'm not sure how to get to that with Glide.


Stephen W_
Giga Guru

This works!   I'm not sure if it's the most efficient method though.



map = gel('variable_map');


var items = map.getElementsByTagName("item");



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


  var item = items.item(i);


  var name = item.getAttribute('qname');


  var id = item.id;


}


Stephen W_
Giga Guru

The solution for this question was a combination of Srikanth's suggestion and something I found elsewhere.


(apologies if there are any mistakes here, I ended up going another route and don't have this exact code anymore)




Catalog Client Script:


OnLoad(){


var scVarElement = {};


var varMap = gel('variable_map');


var items = varMap.getElementsByTagName("item");



//This is the only way I found to get the sys_id for the item_option_new record.


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


  var item = items.item(i);


  var name = item.getAttribute('qname');


  var id = item.id;


    scVarElement = {name:name,id:id,refqualifier:"whateverencodedqueryyouwant"};


}



//Now call an include script method with AJAX


var ga = new GlideAjax('ScItemIntfTypes');


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


  ga.addParam('sysparm_sc_options', scVarElement);


  ga.getXML();


}



Script Include:


setQuestions: function() {


  var grScOptions = new GlideRecord('item_option_new');



  var scQuestion = this.getParameter('sysparm_sc_options');


  if (grScOptions.get(scQuestion.id)){


      grScOptions.reference_qual = scQuestion.refqualifier;


      grScOptions.update();


  }


}




**Note that I ended up switching my 'Lookup Multiple Choice' fields to 'Select Box' fields since setting the reference qualifier would unnecessarily burden the server.   The choice lists are now populated via the client script.