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.

How to convert the string type of duration into minutes/seconds(numeric) for reporting purpose

Priyanka145
Tera Contributor

Hi,

I am having string type of fields which is calculating the difference between 2 date fields and storing into a string type of field.. So, the value appears in the format of days, hours, minutes, seconds.

(ex: 2 days, 10 hour, 45 minutes, 20 seconds)

But for reporting purpose it is not feasible to calculate .Hence , we want to convert into numeric value(minutes/seconds), etc to calculate properly.

(Ex: 6000) . 

So, in excel once report extracted will not face any issue.

Please guide me here, how to get that

 

@Ankur Bawiskar  Could you please guide me

1 ACCEPTED SOLUTION

Hi,

try this

(function executeRule(current, previous /*null when async*/ ) {

    //To display duration between RITM created time and RITM closed time
    var start = new GlideDateTime(current.opened_at); //opened time
    var end = new GlideDateTime(current.closed_at); //closed time
    var dc = new DurationCalculator();
    var sch = gs.getProperty('BusinessHoursSchedule');
    dc.setSchedule(sch); 
    var time = dc.calcScheduleDuration(start, end); 
    var durationMS = time * 1000;
  
    current.u_lifecycle_duration.setDateNumericValue(durationMS);

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

6 REPLIES 6

Hi,

try this

(function executeRule(current, previous /*null when async*/ ) {

    //To display duration between RITM created time and RITM closed time
    var start = new GlideDateTime(current.opened_at); //opened time
    var end = new GlideDateTime(current.closed_at); //closed time
    var dc = new DurationCalculator();
    var sch = gs.getProperty('BusinessHoursSchedule');
    dc.setSchedule(sch); 
    var time = dc.calcScheduleDuration(start, end); 
    var durationMS = time * 1000;
  
    current.u_lifecycle_duration.setDateNumericValue(durationMS);

})(current, previous);

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hi @Ankur Bawiskar , thanks for the script, So, this script works only if the type of field(u_lifecycle_duration) is Duration.