Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Calculate the total duration hours.

Siva82
Tera Expert

Hi all,

 

I am try to calculate the total hours of duration in background. I wrote script in background script. It getting all duration values from different records. But I need sum of the total duration and convert in to Mints or seconds.

Can you please help me on that.

 

I am using below script, and for reference I have attached screenshot

//var dur = [];
//var sum_dur;
var outages = new GlideRecord('cmdb_ci_outage');
//outages.addEncodedQuery('sys_created_onONLast 12 months@javascript:gs.beginningOfLast12Months()@javascript:gs.endOfLast12Months()^cmdb_ci=' + current.dimension.configuration_item);
outages.addEncodedQuery('cmdb_ci=c44f4c5447cc5114a6954d68436d433a');
outages.query();
while(outages.next()){
    //dur = dur.push(outages.getValue('duration'));
   var dur = outages.getDisplayValue('duration');      
sum_dur = dur;
gs.info(dur);
}
Siva82_0-1692249462972.png

 

Thank you,

Siva

1 ACCEPTED SOLUTION

Hello @Siva82 ,

My bad can you try with this script please?

 

var dur=0;
var gr = new GlideRecord('cmdb_ci_outage');
gr.query();
while(gr.next())
{
dur=dur+gr.duration.dateNumericValue()/(60*60*1000);
 

}
gs.info(dur);



 

 

Please add your queries accordingly and run the script

 

This gave the result in total hours as i converted it to hours 

 

Hope this helps 

Mark my answer correct if this helps you 

Thnkas

View solution in original post

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

Hello @Siva82 ,

var dur ='';
var gr = new GlideRecord('cmdb_ci_outage');
gr.query();
while(gr.next())
{

dur =new GlideDuration(gr.getDisplayValue('duration'));
 dur.add(dur);
}

gs.info(dur.getDisplayValue());


 

This worked for me in the background script which gave the total sum of duration values.

 

Note: Add your encoded query and try once.

 

Hope this helps 

 Mark my answer correct if this helps you 

Thanks

Hi Mohith,

Thank you for response,

I using this code its getting wrong hours,

It showing 19586 Days 6 Hours 39 Minutes,but actual hours 2 hours 1 min only,

thank you

Hello @Siva82 ,

My bad can you try with this script please?

 

var dur=0;
var gr = new GlideRecord('cmdb_ci_outage');
gr.query();
while(gr.next())
{
dur=dur+gr.duration.dateNumericValue()/(60*60*1000);
 

}
gs.info(dur);



 

 

Please add your queries accordingly and run the script

 

This gave the result in total hours as i converted it to hours 

 

Hope this helps 

Mark my answer correct if this helps you 

Thnkas

@Siva82 Did the script solve the issue ?