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.

Convert Duration field into day

ektakumari
Tera Contributor

Hello All,

I am working in scoped application and have to convert duration field into days not in hours or minute. it is giving the duration in like. I wants to convert minutes or hours in Days only.  I have tried using getDay or getDayPart/getNumeric value function but they gives the result. Any suggestion.

 find_real_file.png

I have written a business rule and code is :-

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

var opened = new GlideDateTime(current.opened_at.getDisplayValue());
gs.info("open time is " +current.opened_at.getDisplayValue());
var closed = new GlideDateTime(current.closed_at.getDisplayValue());
gs.info("close time is " +current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(opened, closed);
gs.info("Duration is " + duration);
current.calendar_duration = duration;
current.update();


})(current, previous);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@ektakumari

If it is duration then it would show in that format only.

Even with max_unit attribute it would show in partial format.

Not possible to hold only days value.

If that is required for your reporting purpose I would suggest to create new field and store the days into that

Note: creating field will have cost implication as per your servicenow subscription

To get only days from the duration

Avoid current.update() in business rules

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

var opened = new GlideDateTime(current.opened_at.getDisplayValue());
gs.info("open time is " +current.opened_at.getDisplayValue());
var closed = new GlideDateTime(current.closed_at.getDisplayValue());
gs.info("close time is " +current.closed_at.getDisplayValue());
var duration = GlideDateTime.subtract(opened, closed);
gs.info("Duration is " + duration);

var days = (duration.dateNumericValue())/86400000; // this can be integer or float as well

current.<new_field> = days;
current.update();


})(current, previous);

Regards
Ankur

 

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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@ektakumari 

Thanks for marking my response as helpful.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

 

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