Auto populate Duration field

Lucky1
Tera Guru

Hello all,

 

On the Change request, whenever the Actual End is populated by the system, then the difference between Actual Start and Actual End has to be auto populated in the Duration field (Which contains Days and Hours).

 

Note: Duration field backend name: (u_duration)

 

Can someone tell me how can I do this?

 

 

Regards,

Lucky

1 ACCEPTED SOLUTION

@Lucky1 

it worked for me with the 1st script I shared

(function executeRule(current, previous) { // Add your code here
    var startDate = new GlideDateTime(current.start_date);
    var endDate = new GlideDateTime(current.end_date);
	current.u_duration = GlideDateTime.subtract(startDate, endDate);
})(current, previous);

BR

AnkurBawiskar_0-1746766362643.png

 

Output: I gave 2 hours difference, it populated 2 hours, I gave 2mins difference, it populated 2mins

duration field change request.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

View solution in original post

7 REPLIES 7

@Lucky1 

it worked for me with the 1st script I shared

(function executeRule(current, previous) { // Add your code here
    var startDate = new GlideDateTime(current.start_date);
    var endDate = new GlideDateTime(current.end_date);
	current.u_duration = GlideDateTime.subtract(startDate, endDate);
})(current, previous);

BR

AnkurBawiskar_0-1746766362643.png

 

Output: I gave 2 hours difference, it populated 2 hours, I gave 2mins difference, it populated 2mins

duration field change request.gif

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

@Lucky1 The end date/time should be after the start date/time. In your picture, I can see that the end date is before the start date, which is incorrect. Additionally, the duration field is negative, which is why instead of 00:02:00, you are getting 23:58:00 (reverse).

Yes SIva,

 

I am really Sorry not to had an eye on it.

Thanks a lot.

🙂

 

Regards,

Lucky.