get row count

ravi2k
Kilo Contributor

Hi Frds,

The following script(s) use the function getRowCount() to count the number of records in a table. This can lead to performance issues on tables with undetermined number of rows. Instead of getRowCount(), I want to use GlideAggregate , could you pls help to modify the script for me. Thanks.

var ScopeChecker = Class.create();

ScopeChecker.prototype = {         initialize: function() {      

},

getSysIdByScopeName: function(scopeName) {

        if (gs.nil(scopeName) || "global" == scopeName)

                    return null;

        var gr = new GlideRecord("sys_store_app");

        if (!gr.isValid())

        return null;

        gr.addQuery("scope", scopeName);

        gr.query();

        if (gr.next() && gr.getRowCount() == 1)

                  return gr.sys_id.toString();

        return null;

},

isStoreApp: function(scopeName) {

var sys_id = this.getSysIdByScopeName(scopeName);

return !gs.nil(sys_id); },      

type: 'ScopeChecker' };

2 REPLIES 2

Deepa Srivastav
Kilo Sage

Is it a scoped application ? Try below:-



var ScopeChecker = Class.create();


ScopeChecker.prototype = {         initialize: function() {      


},


getSysIdByScopeName: function(scopeName) {


        if (gs.nil(scopeName) || "global" == scopeName)


                    return null;


var gr = new GlideAggregate("sys_store_app");


        if (!gr.isValid())


        return null;


        gr.addQuery("scope", scopeName);


gr.addAggregate('COUNT');


        gr.query();


        if (gr.next() && gr.getAggregate('COUNT') == 1)


                  return gr.sys_id.toString();


        return null;


},


Scoped GlideAggregate API Reference - ServiceNow Wiki



Mark Correct if it solved your issue or hit Like and Helpful if you find my response worthy.


karthiknagaramu
Kilo Sage

Hi,



Try below.


var gr= new GlideAggregate('sys_store_app');


gr.addQuery("scope", scopeName);


gr.addAggregate('COUNT');


gr.query();


var result= 0;


if (gr.next())


    result= gr.getAggregate('COUNT');



Instead of using GlideRecord.



Regards,


Karthik Nagaramu