How can I access the variable pool of a referenced item to populate a record producer field

Robert Beeman
Kilo Sage

I am working with a record producer for a custom table. One of the variables is a reference field to the sc_req_item table. Once an RITM is selected, we would like it to auto-populate some additional fields with data held in variables of that RITM. For example, one of the variables is called 'nh_outside_consultant'. On server-side scripting i'm able to dot walk to it like this 'item.variables.nh_outside_consultant', but I'm unsure how to accomplish this through a catalog client script. I'm able to query the glide record of the item, but the variables are not available. Are they held in another table? Thank you for any advice you can give me.

1 ACCEPTED SOLUTION

In your onChange client script, you will need something like this:



var ga = new GlideAjax('HelloWorld');


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


ga.addParam('sysparm_item', newValue);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


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


    alert(answer);


}




Then you will need to create a script include that is going to look something like this:



var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    helloWorld: function() {


          var ritm = this.getParameter('sysparm_item');


          var itemGR = new GlideRecord('sc_req_item');


          itemGR.get(ritm);


          return itemGR.variables.nh_outside_consultant;


    },



});





If you follow the tutorial correctly, this should alert that variable assuming you selected the correct RITM. What we are doing in the client script is calling our script include with GlideAjax. Inside of the script include, we are running a GlideRecord query on the sc_req_item table that will get the particular RITM selected. Then we return that variable you provided.


View solution in original post

4 REPLIES 4

mitchell_stutle
Mega Expert

Hey Robert,



You will need to use a Glide Ajax call. Here is a wiki article that demonstrates how to do this:



GlideAjax - ServiceNow Wiki



You will need to pass the sys_id of the RITM and have the server return whatever variables you need. Please let me know if any of the tutorial is unclear!


Hello Mitchell,



Thank you for the quick reply. I suspected that I would have to use GlideAjax. I haven't worked with this type of call before. Unfortunately, the sample doesn't show how it queries and returns data from other tables. I'll see if I can figure it out. I'd love to see a more real world example.


In your onChange client script, you will need something like this:



var ga = new GlideAjax('HelloWorld');


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


ga.addParam('sysparm_item', newValue);


ga.getXML(HelloWorldParse);



function HelloWorldParse(response) {


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


    alert(answer);


}




Then you will need to create a script include that is going to look something like this:



var HelloWorld = Class.create();


HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    helloWorld: function() {


          var ritm = this.getParameter('sysparm_item');


          var itemGR = new GlideRecord('sc_req_item');


          itemGR.get(ritm);


          return itemGR.variables.nh_outside_consultant;


    },



});





If you follow the tutorial correctly, this should alert that variable assuming you selected the correct RITM. What we are doing in the client script is calling our script include with GlideAjax. Inside of the script include, we are running a GlideRecord query on the sc_req_item table that will get the particular RITM selected. Then we return that variable you provided.


Awesome! I think this will get me to what I need. I appreciate your help.