Gliding two tables for diff outputs

servicenow14710
Tera Expert

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;

3 REPLIES 3

AshishKM
Kilo Patron
Kilo Patron

Hi @servicenow14710 , 

You can use GlideAggregate() API for counting the record based on some column name.

 

Refer the below API details.

https://developer.servicenow.com/dev.do#!/reference/api/washingtondc/server_legacy/c_GlideAggregateA...

 

-Thanks,

AshishKM


Please mark this response as correct and helpful if it helps you can mark more that one reply as accepted solution

Ayushi12
Mega Sage

Hi @servicenow14710,

Use GlideAggregate() ServiceNow class for COUNT and SUM.

Refer:https://www.basicoservicenowlearning.in/2023/04/glideaggregate.html#:~:text=GlideAggregate%20is%20a%....

Please mark this response as correct or helpful if it assisted you with your question.

Rohant Joshi
Tera Contributor

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);
  }