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

poyntzj
Kilo Sage

I would try it manually as a Background script and see what happens then (replace MYLAPTOP and T440 with a real device and or model number as per your cmdb_model table)



encQuery = "nameLIKEMYLAPTOP^ORmodel_numberLIKET440,  


gr = new GlideRecord("cmdb_model");  


gr.addEncodedQuery(String(encQuery));  


gr.query();  


while (gr.next())


{


gs.print(cmdb_model.sys_id);


}



At least that will confirm you get data back via your query.   You can then progress from there.


Hmm, I've run this and I get a lot more returned records than I should be, which tells me some thing is wrong with this.


there might be something wrong with the query then. Please try same query on the table to cross verify.


I'd work on looking and getting it to return the a device name you know of and a model number where there are just a few entries (at the end of the encQuery line, make sure it has the " - missed it off in the reply above)



Once I know that works I would look at your client script and make it use the hard coded encQuery line from above - as at least this way you know what should be returned and can check the rest of the code is good.


Once you are there, change the line back to using your original line



Also when the encQuery line is created, add


g_form.addInfoMessage('Encoded Query : ' + encQuery);


You can then see the actual query line it is going to use (and if needed check that via the background script too)


I should be getting one return with the values I gave (which I've checked in the breadcrumb filter on the table itself) but I get 2541 returns when doing the same query in this background script. Very strange!