How to populate duration type of field's value on form

akshay parekar1
Tera Contributor

Hello,

i have written a after business rule on insert,update for my custom table

i'm getting difference between two date/time type of fields ,but its not getting populated in my duration type of field

i want it to get populated as (eg. 14 04:02:56)

i'm sharing business rule, tell what i have to change

(function executeRule(current, previous /*null when async*/ ) {
    var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
    var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
    var output =  gs.dateDiff(fromdate, todate);
        gs.addInfoMessage(fromdate);
        gs.addInfoMessage(todate);
        gs.addInfoMessage(output);
    current.u_required_duration= output.getByFormat();


})(current, previous);

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @akshay parekar 

Please make your BR as Before Insert/Update and update script as this

If you keep your BR as after then you will have to use current.update() in your BR which is not good.

(function executeRule(current, previous /*null when async*/ ) {
	
	var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
	var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
	
	var duration = GlideDateTime.subtract(fromdate,todate);
	current.u_required_duration = duration;

})(current, previous);

Regards
Ankur

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

View solution in original post

5 REPLIES 5

Martin Ivanov
Giga Sage
Giga Sage

There is a GlideDuration API. Please see the docs.

You can also check some of the OOTB business rules for an example. e.g (Calculate Wrap Up Duration)

(function executeRule(current, previous /*null when async*/) {	
	if (current.isActionAborted() || current.state_changed_on.nil() || current.closed_at.nil()) {
		return;
	}

	var wrap_up_start = current.state_changed_on.dateNumericValue();
	var wrap_up_end = current.closed_at.dateNumericValue();
	
	current.wrap_up_duration = new GlideDuration(wrap_up_end - wrap_up_start);

})(current, previous);

Please mark Correct and Helpful if my answer helps you resolve your issue. Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

vikas shukla1
Tera Contributor

Hi Akshay,

Please refer the below link , it will helpful for you.

Link - How to calculate difference between two dates and time

Thanks,

Vikas 

Martin Ivanov
Giga Sage
Giga Sage

Please mark Correct and Helpful if my answer helps you resolve your issue.

Тhis will close the thread and other users may also benefit from it.

Thanks!
Martin Ivanov
Community Rising Star 2022


Please mark Correct and click the Thumb up if my answer helps you resolve your issue. Thanks!
Martin Ivanov
ServiceNow MVP 2023, 2024

Ankur Bawiskar
Tera Patron
Tera Patron

Hi @akshay parekar 

Please make your BR as Before Insert/Update and update script as this

If you keep your BR as after then you will have to use current.update() in your BR which is not good.

(function executeRule(current, previous /*null when async*/ ) {
	
	var fromdate = new GlideDateTime(current.getValue('u_start_date')); //to get start date value
	var todate = new GlideDateTime(current.getValue('u_end_date')); //to get end date value
	
	var duration = GlideDateTime.subtract(fromdate,todate);
	current.u_required_duration = duration;

})(current, previous);

Regards
Ankur

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