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.

unable to populate duration properly

chaithanya03091
Tera Contributor

i am trying to get average of an sla duration   and trying to populate as text but it is giving me improperformat and imoroper values 

chaithanya03091_0-1726688375329.png

 

3 REPLIES 3

Gangadhar Ravi
Giga Sage

Hi @chaithanya03091  Please try below updated script. I tried this in PDI and working as expected. Update query as per your need. 

 

var durationGR = new GlideRecord('task_sla');
durationGR.addEncodedQuery('task=ef43c6d40a0a0b5700c77f9bf387afe3');
durationGR.query();

var totalDurationMillis = 0;
var recordCount = 0;

while (durationGR.next()) {
    var durationStr = durationGR.getValue('business_duration');
    if (durationStr) {
        var durationObj = new GlideDuration(durationStr);
        totalDurationMillis += durationObj.getNumericValue();
        recordCount++;
    }
}

if (recordCount > 0) {
    var averageDurationMillis = totalDurationMillis / recordCount;
    var averageDuration = new GlideDuration(averageDurationMillis);

    var days = Math.floor(averageDuration.getNumericValue() / (1000 * 60 * 60 * 24));
    var hours = Math.floor((averageDuration.getNumericValue() % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
    var minutes = Math.floor((averageDuration.getNumericValue() % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = Math.floor((averageDuration.getNumericValue() % (1000 * 60)) / 1000);

    var formattedDuration = days + " days, " + hours + " hours, " + minutes + " minutes, " + seconds + " seconds";
    gs.info("Average SLA Duration: " + formattedDuration);
} else {
    gs.info("No SLA records found for the given query.");
}

 

Please mark my answer correct and helpful if this works for you.

no it is giving me the following answer 

chaithanya03091_0-1726690282744.png

 

Gangadhar Ravi
Giga Sage

@chaithanya03091  try this 

 

var list = new GlideRecord("task_sla");
list.addEncodedQuery("task=ef43c6d40a0a0b5700c77f9bf387afe3");
list.setLimit(10);
list.query();
while (list.next()){
    gs.info(list.business_duration.getDisplayValue());
}