Encoded query is not working in catalog client script

Musab Rasheed
Tera Sage
Tera Sage

Hi,

My Encoded query is not working , basically I'm glide recording RITM table to check whether record of same name created in last 30 days exist or not , if this encoded query will not work in client side please help to write script include for the same. below pasting my client script code.

function onSubmit() {

var companyname = g_form.getValue('req_company');

  var pacitemname = g_form.getValue('pac_insert');

  var grb = new GlideRecord('sc_req_item');

  grb.addQuery('u_pac_item', pacitemname);

  grb.addQuery('u_requested_for_company', companyname);

  grb.addQuery('price', '=', '0.00');

  //grb.addEncodedQuery('opened_atONLast 30 days@javascript:gs.daysAgoStart(30)@javascript:gs.daysAgoEnd(O)') //This line of code is not working

  grb.query();

  if(grb.next())

  {

  alert('Please select some other PAC Item as you have already selected it earlier for this month');

  return false;

  }

}

Thanks in Advance

Please hit like and mark my response as correct if that helps
Regards,
Musab
4 REPLIES 4

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Musab,



You can generate the complete query via filter. Please check section 3 here for more info.


Encoded Query Strings - ServiceNow Wiki



Also it is best practice to switch to GlideAjax instead of making GlideRecord calls from client script.


GlideAjax - ServiceNow Wiki  


Kalaiarasan Pus
Giga Sage

Encoded query will not work in client side glide record. You will need to replace this with Script include+Glideajax.


Hi K,



You can still use an encoded query in client-side GlideRecords, if you add it via "gr.addQuery(...)" with a single argument (the encoded query string).



But you're correct in that "gr.addEncodedQuery(...)" is not a supported method for the client-side GR.



Client Side GlideRecord - ServiceNow Wiki




Thanks,


-Brian


srinivasthelu
Tera Guru

Hi Musab,



You need to convert it to GlideAjax something like this,



function onSubmit() {



var companyname = g_form.getValue('req_company');


  var pacitemname = g_form.getValue('pac_insert');


var ga = new GlideAjax('ValidateScript');


ga.addParam('companyname', companyname);


ga.addParam('pracitemname',pacitemname);


ga.getXMLWait();


if(ga.getAnswer()=='true');


  {


  alert('Please select some other PAC Item as you have already selected it earlier for this month');


  return false;


  }



}





Create below script include with name ValidateScript



var ValidateScript = Class.create();


ValidateScript.prototype = Object.extendsObject(AbstractAjaxProcessor, {




validate: function(){


var companyname =this.getParameter('companyname');


var pacitemname=this.getParameter('companyname');



var grb = new GlideRecord('sc_req_item');


  grb.addQuery('u_pac_item', pacitemname);


  grb.addQuery('u_requested_for_company', companyname);


  grb.addQuery('price', '=', '0.00');


  //grb.addEncodedQuery('opened_atONLast 30 days@javascript:gs.daysAgoStart(30)@javascript:gs.daysAgoEnd(O)') //This line of code is not working


  grb.query();


  if(grb.next())


  {


  return false;  


  }


  return true;


},



      type: 'ValidateScript'


});




Regards


Srinivas