Querying Related Tables sc_req_item and sc_cat_item

kailashthiyagar
Kilo Guru

My requirement is to pass in the "Requested Item number" and "requested item name" and check whether any records exist...

Noticed that name field is present in different table sc_cat_item... I m passing in the Requested item number and name from my client script.. This is the below code i m using...

but do we have an option to do dot walking and do this validation.. i tried it but it doesnt work... could any suggest how we can do it in simplied way?

var reqDetails = new GlideRecord('sc_req_item');

  //var catItem = reqDetails.addJoinQuery('sc_cat_item');

  reqDetails.addQuery('number',g_form.getValue('request_number_2'));

  //catItem.addCondition('name', 'Hardware Exception Appeal');

  reqDetails.query();

  //create and return the array of environments

  while(reqDetails.next()){

  var catItemData=new GlideRecord('sc_cat_item');

  catItemData.addQuery('sys_id',reqDetails.cat_item);

  catItemData.query();

  while(catItemData.next()){

  //do the logic over here

  }

}

25 REPLIES 25

Yes Sourabh.. we are not able to get the below one working.. its coming as undefined... So we are making second glide record query... we thought this dot notation should fetch the value but somehow its not..



gs.addInfoMessage(reqDetails.cat_item.name);



"reqDetails.cat_item" gives the Sys ID....


Then with the Sys ID, we are making second query to get the name... Its round about option:(


Hi Kailash,



I have already tested this script in business rule and it works well for me. Try writing a script include, pass the request number to the script include, and try returning the name of the cat_item. It has to work. Make sure to print out all the values you passed so that you are sure that you are passing correct values.



IF possible paste your client script and script include here so that I can test.



Kind regards,


Sourabh Dhaygude


This is the script include and client script which i m using.. For testing purpose, i m just returning the name from the script include and adding an alert in the client script.. Hope that is clear.



Here is the script include



validateRequestNumber: function(){


  var answer=false;


  var reqNo = this.getParameter('sysparm_req_number');


  var reqDetails = new GlideRecord('sc_req_item');


  reqDetails.addQuery('number',reqNo);


  reqDetails.query();


  //create and return the array of environments


  while(reqDetails.next()){


  return reqDetails.cat_item.name;


}


}




Here is the client script



var ga = new GlideAjax('EUTSCatalogRITMValidation');
var reqNo=g_form.getValue('request_number_2');
ga.addParam('sysparm_name','validateRequestNumber');
ga.addParam('sysparm_req_number',reqNo);
ga.getXMLAnswer(validateReqNo);




function validateReqNo(response) {


  alert(response);


}


Can you paste your entire script include here. You are missing class declarations here.   Did you check the client callable checkbox on the script include?


Hi i have included client callable in my script include




var EUTSCatalogRITMValidation = Class.create();


EUTSCatalogRITMValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateRequestNumber: function(){


  var answer=false;


  var reqNo = this.getParameter('sysparm_req_number');


  var reqDetails = new GlideRecord('sc_req_item');


  reqDetails.addQuery('number',reqNo);


  reqDetails.query();


  //create and return the array of environments


  while(reqDetails.next()){


  return reqDetails.cat_item.name;


  }


  return answer;


  },


  type: 'EUTSCatalogRITMValidation'


});