Total of Array Values

Rshear
Kilo Expert

Dear ServiceNow friends and Experts!

Firstly I have to admit Arrays are not my strong point at all.

I'm looking to display the total business impact time looking at all business services impacted for a Major Incident.

I have a table (task_cmdb_ci_service) which contains the business services and also allows to input total duration of outage. I need to look at all those records on that table that are associated to a MI and output the total of all the duration on all of the task_cmdb_ci_service records.

Im doing this on a B.R on the main incident table (and will not have an issue technically putting the total duration value there) issue is I cant get to the total!

Here is my feable attempt:

\\get the outage records associated with the Major Incident

var gr = new GlideRecord('task_cmdb_ci_service');

  gr.addQuery('task',current.sys_id);

      gr.query();

where there is an outage record find the duration and output it to a numeric value

while (gr.next()) {

  var duration = gr.u_duration.dateNumericValue();

  gs.log("Impact duration " +duration);

\\build array of values and add them up!

  var arr = [];

  arr = duration;

      var total = 0;

  for (var i = 0; i < arr.length; i++) {

  gs.log("Impact total duration is " +total);

  total +=arr[i];

}

}

I haven't written the piece to put them on the incident record but dont see that being an issue if I can get at it.

Any help is much appreciated

1 ACCEPTED SOLUTION

edwin_munoz
Mega Guru

Hello Russell,



I think this should do it:



var total = 0;




var gr = new GlideRecord('task_cmdb_ci_service');


gr.addQuery('task',current.sys_id);


gr.query();



while (gr.next()) {


  var duration = gr.u_duration.dateNumericValue();


  gs.log("Impact duration " +duration);


  total += duration;




}


View solution in original post

3 REPLIES 3

edwin_munoz
Mega Guru

Hello Russell,



I think this should do it:



var total = 0;




var gr = new GlideRecord('task_cmdb_ci_service');


gr.addQuery('task',current.sys_id);


gr.query();



while (gr.next()) {


  var duration = gr.u_duration.dateNumericValue();


  gs.log("Impact duration " +duration);


  total += duration;




}


Deepak Ingale1
Mega Sage

\\get the outage records associated with the Major Incident


var gr = new GlideRecord('task_cmdb_ci_service');


  gr.addQuery('task',current.sys_id);


      gr.query();



where there is an outage record find the duration and output it to a numeric value


while (gr.next()) {


  var duration = gr.u_duration.dateNumericValue();


  gs.log("Impact duration " +duration);



\\build array of values and add them up!


  var arr = [];


  arr = duration;      


      var total = 0;


  for (var i = 0; i < arr.length; i++) {


  gs.log("Impact total duration is " +total);


  total +=arr[i];


}


}


l


\\get the outage records associated with the Major Incident


  var arr = [];
  var total = 0;


var i = 0;
new GlideRecord('task_cmdb_ci_service');


  gr.addQuery('task',current.sys_id);


      gr.query();



where there is an outage record find the duration and output it to a numeric value


while (gr.next())


{


  var duration = gr.u_duration.dateNumericValue();


  gs.log("Impact duration " +duration);



\\build array of values and add them up!




        arr[i] = duration ;           //arr is array and hence here you shud have used something like arr[0] = duration


        total = total + arr[i]
  gs.log("Impact total duration is " +total);


  i++ ;


}


Can you please try with 2nd code and let me know?


Both...really appreciate your help on this...solution provided was excellent!! and did the trick"!.


This, its seems was a case of me trying to over complicate things as usual.


Thanks again.