Access request item variables through client script

manredd
Kilo Expert

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

if (isLoading) {

  return;

}

var Emp = new GlideRecord('sc_req_item');

Emp.addQuery('sys_id', newValue);

Emp.query();

while(Emp.next()) {

  g_form.setValue('u_first_name', Emp.variables.u_first_name);

    }

}

i used this code but this gives me undefined in my coloumn

1 ACCEPTED SOLUTION
14 REPLIES 14

Hi Manohar,



when you open a new catalog form, it doesn't initially create a Request or Request Item. It only creates these when you submits the catalog form and in that case all the variables you filled in the catalog form gets linked to the Request Item created.



So if user want to use the variables of a Rejected Request Item, He/she will have to provide the reference of that RITM and then you can use GlideAJAX along with Script include to fetch the variable data from that RITM.   Please use GlideAjax - ServiceNow Wiki to get more information on how to use GlideAjax and Script include.



Hope it would help.



Regards,


Ishant


sachin312
Giga Expert

Try this out.


Client script:



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


  if (newValue == ' ') {


  return;


  }



var ga = new GlideAjax('GetRequestedItemVariablesAjaxJSON');


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


  ga.addParam('sysparm_cat_id',g_form.getValue('newValue'));


  ga.getXML(ajaxResponse);


function ajaxResponse(response){


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


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


              g_form.setValue('u_first_name',answer[i].name);


}


}




script include:



Name: getnamevalue



var getnamevalueAjaxJSON = Class.create();


getnamevalueAjaxJSON.prototype = Object.extendsObject(AbstractAjaxProcessor, {


  getUsersValues: function()   {


  var cat_id = this.getParameter('sysparm_cat_id');


  var gr = new GlideRecord('sc_req_item');


  var answer = [];


  gr.addQuery('sys_id',cat_id);


  gr.query();


  if(gr.next()){


  answer.push({


                        'name': gr.variables.u_first_name +'',


                        });


}


return new JSON().encode(answer);


      },


type: 'getnamevalueAjaxJSON'


});


manredd
Kilo Expert

Thank you guys for the help


What was your end solution to this? Thanks.