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

J Siva
Tera Sage

Hi @Lucky1 
Try "Before Business rule" as belwo to calculate the duration.

JSiva_0-1746604525848.png

JSiva_1-1746604539587.png

(function executeRule(current, previous /*null when async*/ ) {
    var gdtStart = new GlideDateTime(current.getValue('work_start'));
    var gdtEnd = new GlideDateTime(current.getValue('work_end'));

    var duration = GlideDateTime.subtract(gdtStart, gdtEnd); //the difference between gdt1 and gdt2

    current.u_duration = duration.getDurationValue();

})(current, previous);

Output:

JSiva_2-1746604608572.png

 

Regards,
Siva

Ankur Bawiskar
Tera Patron
Tera Patron

@Lucky1 

you can use before update business rule to populate the custom duration field

(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);

OR

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

})(current, previous);

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
Tera Guru

Thank you Siva and Ankur,

 

Wil check and get back.

 

 

Regards,

Lucky

Lucky1
Tera Guru

Hello Ankur and Siva,

 

Good day!!

I have tried both of your logics but it's not updating with correct value.

From the below pic, it has to be updated with 2 mins only.

 

Lucky1_0-1746765117195.png

I put the log message for duration, and it is showing

Lucky1_1-1746765560955.png

 

 

Can you please guide me?

 

 

Regards,

Lucky