Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

I am not getting correct answer through encoded query in glide record

Murtaza Saify
Tera Contributor
function onLoad() {

    var gr = new GlideRecord('cmdb_ci');
    gr.getEncodedQuery('cost>10000');
    gr.query();
    var str;
    var count=0;
    while (gr.next()) {
        str = str+ ' and  ' + gr.name;
        count++;
    }
    alert(str);
    alert(count);

}
 
Here is my code  whats wrong with it
17 REPLIES 17

Community Alums
Not applicable

Hi @Murtaza Saify 

Try addQuery instead of addEncodedQuery.

function onLoad() {

    var gr = new GlideRecord('cmdb_ci');
    gr.addQuery('cost', '>', '10000');
    gr.query();
    var str;
    var count=0;
    while (gr.next()) {
        str = str+ ' and  ' + gr.name;
        count++;
    }
    alert(str);
    alert(count);

}

Sohail Khilji
Kilo Patron

Try this :

 

Script include : getCount

getRecCount : function(){

var counts = [];

 var gr = new GlideRecord('cmdb_ci');
    gr.getEncodedQuery('cost>10000');
    gr.query();
    var str;
    var count=0;
    while (gr.next()) {
        count.push(str+ ' and  ' + gr.name);
        count++;
    }
  return counts.toString();
}

 

 

 


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect

Community Alums
Not applicable

Hi @Murtaza Saify ,

Please try below code 

    var gr = new GlideRecord('cmdb_ci');
    gr.addEncodedQuery('cost>10000');
    gr.query();
    var name = [];
    var count = 0;
    while (gr.next()) {
        name.push(gr.name.toString());
        count++;
    }
    gs.print("name = " + name);
    gs.print("count = " + count);

Result 

SarthakKashyap_0-1717508124997.png

 

Please mark my answer correct and helpful if this works for you

Thanks and Regards 

Sarthak