Gliding two tables for diff outputs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2024 02:53 AM - edited ‎02-24-2024 02:53 AM
Hello developers, please suggest me with servicenow best practice in using two gliderecords for two different tables to get count of both and return addition of those counts. Any help is appreciated. Thanks!
var gr1 = new GlideRecord('table_A');
gr1.addEncodedQuery('xyz');
gr1.query();
var count1 = gr1.getRowCount();
alert('count1');
var gr = new GlideRecord('table_B');
gr.addQuery('active', true);
gr.query();
var count = gr.getRowCount();
var countBoth = count + count1;
return countBoth;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2024 03:40 AM
Hi @servicenow14710 ,
You can use GlideAggregate() API for counting the record based on some column name.
Refer the below API details.
-Thanks,
AshishKM
Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2024 03:52 AM
Hi @servicenow14710,
Use GlideAggregate() ServiceNow class for COUNT and SUM.
Please mark this response as correct or helpful if it assisted you with your question.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-27-2024 04:36 AM
I see you have been advised to use GlideAggregate API, just adding sample script for quick reference:
var aggIncident = new GlideAggregate('incident');
aggIncident.addAggregate('COUNT');
aggIncident.query();
while(aggIncident.next()){
var msg = current.caller_id.name + " has " + aggIncident.getAggregate('COUNT') + " active incidents." ;
gs.addInfoMessage(msg);
}