Count the amount of records from the table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 06:16 AM
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
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 06:23 AM
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
Hope this helps,
Shane

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 06:30 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 06:26 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-26-2022 06:51 AM
Hi
You can follow the approaches suggested by
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