How to calculate the percentage by script of two queries

MR Carvalho
Tera Contributor

Hi guys,

 

 

I would like to calculate by script the percentage in two different queries that came from the same glide aggregate, so I want to calculate the count of my incident that is category = bussiness and divided by the total incident.

 

Below following an example.

 

 

 

var incidentGA = new GlideAggregate('incident_sla');

incidentGA.addAggregate('COUNT');
incidentGA.query();

while (incidentGA.next()) {

 

var percentage = (incidentGA.addEncodedQuery(category=Business) / incidentGA) * 100 // imagine that in my example the count of query is 10 divided by the total that is 100, so in the end I'll divided by 100 to get the percentage calculation.

 

gs.print(percentage ); // In this example the result should be 10.

}

 

Please, could someone help me?

 

Thanks in advance.

3 REPLIES 3

Bert_c1
Kilo Patron

Hi Gle,

 

Please refer to:

 

https://developer.servicenow.com/dev.do#!/reference/api/utah/server_legacy/c_GlideAggregateAPI?navFi...

 

Your script fails for me:

 

Evaluator: com.glide.script.RhinoEcmaError: "Business" is not defined.
   script : Line(8) column(0)
      5: 
      6: while (incidentGA.next()) {
      7: 
==>   8: var percentage = (incidentGA.addEncodedQuery(category=Business) / incidentGA) * 100; // imagine that in my example the count of query is 10 divided by the total that is 100, so in the end I'll divided by 100 to get the percentage calculation.
      9: 
     10:  
     11: 

Hi @Bert_c1 1,

Yes my code is not working, thats what I'm looking for, below I'll give another example for what I need, in red is the line that I need help with the code.

 

 

var incidentGA = new GlideAggregate('incident');

incidentGA.addAggregate('COUNT');
incidentGA.query();

while (incidentGA.next()) {

 

var percentage = Here I need to calculate variable "incidentGA" filter the category=business / incidentGA (with no filter) * 100

 

gs.print(percentage ); 

}

 

 

 

Many Thanks

Bert_c1
Kilo Patron

hi Gle,

 

Did you review the API that I posted?  Your use of GlideAggregate() is wrong. What field on the incident or incident_sla table do you want get an 'aggregate' on? Please review the documentation at the link I posted.