Client Script - addEncodedQuery

Daryll Conway
Giga Guru

I have an onSubmit client script that I am trying to run the following code in ...

function onSubmit() {

      var modelMatch = false,

      curModel = g_form.getValue('u_discovered_model'),

      ciList = [],

      encQuery = "nameLIKE" + String(curModel) + "^ORmodel_numberLIKE" + String(curModel),

      gr = new GlideRecord("cmdb_model");

      gr.addEncodedQuery(String(encQuery));

      gr.query();

      alert('pre query');

      while (gr.next()) {

              ciList.push(String(gr.name));

              modelMatch = true;

      }

      if(modelMatch){

              var listModels = '';

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

                      listModels = listModels + ' : ' + String(ciList[i]);

              }

              if(confirm('Are you sure this model does not already exist?\nThese models are already in Service Now : ' + listModels + '\nClick CANCEL if you would like to change to one of these')){

                      return true;

              }

              else{

                      g_form.showErrorBox("u_discovered_model", 'Please Change the model to an existing one. Such as : ' + listModels);

                      return false;

              }

      }

}

I reach line 6 but it falls over on the encoded query (line 7).

I've checked my query and it is as expected and works when I use it to query the table directly (in list view).

Can anyone see where I'm going wrong?

28 REPLIES 28

test this via a background script.


Change the names to be relevant entries in your table



var gr = new GlideRecord('sys_user');


gr.addQuery('last_name','Poyntz').addOrCondition('last_name','Conway');


gr.query()


while(gr.next())


{


gs.print(gr.name)


}


This works in a back ground script but I'm trying to use this in a client script with no joy.


poyntzj
Kilo Sage

your g_form.getValue... is that field returning the sys_id (from a reference field) or is it a name you have typed in ?


I suspect you are being returned a sys_id and are searching against a name so it will not match


you could change your encodedQuery to be



encQuery = "sys_idLIKE" + curModel;


gr.addEncodedQuery(encQuery);




cheers


Thank you for the suggestion but this is not a referenced field, it is a string value and I've checked that the encoded query string was correct.