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.

Count the amount of records from the table

Kasia5
Tera Contributor

Hi All,

I am trying to get the number of records from table sc_req_item (records from last 30 days). I think I need to create this by Client Script and Script Include because when I will have number of records then I want to add some conditions to check what number of this records have field ABC with value true and then show the field in the form from another table, so I created Client Script and Script INclude like this to count the whole records from sc_req_item, but it doesn't work..:

Client Script:

function onLoad() {

var ajax = new GlideAjax('calculateRITM');

ajax.addParam('sysparm_name', 'getCount');

ajax.getXML(IncCount);

 function IncCount(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

alert(answer);

}

}

 

Script Include:

var calculateRITM = Class.create();

calculateRITM.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getCount: function() {

var gr = new GlideRecord('sc_req_item');
gr.addEncodedQuery('sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()');
gr.query();

var answer = '';

while (gr.next()) {

answer = gr.getRowCount();

}

return answer;

},

type: 'calculateRITM'
});

 

What is wrong?

 

Thanks in advance for help

9 REPLIES 9

Shane41
ServiceNow Employee
ServiceNow Employee

Hi Kasia,

I would recommend creating a display business rule on the incident to get the count of requested items and capture the value in a scratchpad variable

You can then reference that variable in your onload client script

See this post on the g_scratchpad variable

https://community.servicenow.com/community?id=community_question&sys_id=7971cb29db98dbc01dcaf3231f96...

Hope this helps,

Shane

Shane41
ServiceNow Employee
ServiceNow Employee

Something like this

Business Rules

find_real_file.png

find_real_file.png

Client scriptfind_real_file.png

 

 

Sagar Pagar
Tera Patron

Hi,

Your answer = gr.getRowCount() should be outside & before while black.

	getCount: function() {

		var gr = new GlideRecord('sc_req_item');
		gr.addEncodedQuery('sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()');
		gr.query();
		var answer = '';
		answer = gr.getRowCount();

		return answer;

	},

 

As suggested Shane, you can use the display BR and g_scratchpad variable and used it in  on-load client script.

Thanks!

Sagar Pagar

The world works with ServiceNow

Kartik Sethi
Tera Guru

Hi @Kasia 

You can follow the approaches suggested by @Sagar Pagar or @Shane. Your script looks fine but I would not recommend using getRowCount instead go for getAggregate (GlideAggregate)

 

Modified scripts are provided below:

Client Script:

function onLoad() {
    var ajax = new GlideAjax('calculateRITM');
    ajax.addParam('sysparm_name', 'getCount');

    //Use getXMLAnswer as it omits the requirement to fetch value from XML tags
    ajax.getXMLAnswer(IncCount);

    function IncCount(response) {
        alert(response);
    }

}

Script Include:

var calculateRITM = Class.create();
calculateRITM.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getCount: function() {
        //Never use generic variable like "gr"
        var answer = 0,
            reqGA = new GlideAggregate('sc_req_item');

        reqGA.addEncodedQuery('sys_created_onONLast 30 days@javascript:gs.beginningOfLast30Days()@javascript:gs.endOfLast30Days()');
        reqGA.addAggregate('COUNT');
        reqGA.query();

        if (reqGA.next()) {

            answer = reqGA.getAggregate('COUNT');

        }

        return answer;
    },

    type: 'calculateRITM'
});

 


Please mark my answer as correct if this solves your issues!

If it helped you in any way then please mark helpful!

 

Thanks and regards,

Kartik