Client Script - addEncodedQuery

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2015 07:21 AM
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?
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2015 06:51 AM
what if you copy the query from the breadcrumb into the script and then run it.
How many records are in the cmdb_model table ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2015 06:54 AM
I tried this(BG script) on my instance and working fine for me.:
var encQuery = "nameLIKE2094^ORmodel_numberLIKEZ09";
gr = new GlideRecord("cmdb_model");
gr.addEncodedQuery(encQuery);
gr.query();
while(gr.next())
{
gs.print(gr.sys_id);
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2015 07:01 AM
I've tried using the query directly copied from the tables query and I get the same result !?
Using the query on the table I get 1 record in the script I get 2541 !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2015 07:14 AM
odd, working here fine for me.
I tried
var encQuery = "nameLIKE5875^ORmodel_numberLIKEAR3100SP1";
gr.addEncodedQuery(encQuery) and gr.addEncodedQuery(String(encQuery)); incase it threw an error if you used String.
same result for me, 2 records (one matching each of the criteria)
Cheers