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.

Based on Duration field we want to set another field

Sirri
Tera Guru

Hi All,

 

I have requirement:

variable Name                -   Type

1)Duration                      - Duration

2)Due                              -Date/Time

 

Automatically fill the Due field by calculating the Duration mention + Current Date&Time

For understanding:

If suppose Duration selected as 7 Days 1 hour 30 min

If current time is populate by calculation current date time + 7 Days 1 hour 30 min

please find the below field will be available on the form like below

Sirri_0-1701266302481.png

 

 

How can I achieve this . Please let us know.

3 ACCEPTED SOLUTIONS

Tai Vu
Kilo Patron
Kilo Patron

Hi @Sirri 

Let's define an OnChange Client Script for the Duration variable. Then pass the duration to an AJAX Script Include, in which we will convert to second > initiate GlideDateTime > add the seconds > return the date time value.

Sample below.

#Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('due_date');
    }

    var ga = new GlideAjax('CLIncidentUtilsAJAX');
    ga.addParam('sysparm_name', 'calculateDueDate');
    ga.addParam('sysparm_duration', newValue);
    ga.getXMLAnswer(function(response) {
        g_form.setValue('due_date', response);
    });

}

 

#AJAX Script Include

var CLIncidentUtilsAJAX = Class.create();
CLIncidentUtilsAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	calculateDueDate: function(){
		var gd = new GlideDuration(this.getParameter('sysparm_duration'));
		var second = gd.getNumericValue() / 1000;
		var gdt = new GlideDateTime();
		gdt.addSeconds(second);
		return gdt.getValue();
	},

    type: 'CLIncidentUtilsAJAX'
});

 

Cheers,

Tai Vu

View solution in original post

Hi@Tai Vu ,

 

Thank you, please let us know why I'm getting time different 

please see below snip you will get an idea. I have selected duration 7 hours but in due date i'm getting 22 hours . But current time is 7 hr 46 min. Please let us know If still I want to modify any thing.

Sirri_0-1701272894411.png

 

View solution in original post

Hi @Sirri 

It's because the difference between user's timezone and the system timezone (UTC by default).

let's change the line return from the function in the script include.

From

return gdt.getValue();

To

return gdt.getDisplayValue();

 

Cheers,

Tai Vu

View solution in original post

4 REPLIES 4

Tai Vu
Kilo Patron
Kilo Patron

Hi @Sirri 

Let's define an OnChange Client Script for the Duration variable. Then pass the duration to an AJAX Script Include, in which we will convert to second > initiate GlideDateTime > add the seconds > return the date time value.

Sample below.

#Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading) {
        return;
    }

    if (newValue === '') {
        g_form.clearValue('due_date');
    }

    var ga = new GlideAjax('CLIncidentUtilsAJAX');
    ga.addParam('sysparm_name', 'calculateDueDate');
    ga.addParam('sysparm_duration', newValue);
    ga.getXMLAnswer(function(response) {
        g_form.setValue('due_date', response);
    });

}

 

#AJAX Script Include

var CLIncidentUtilsAJAX = Class.create();
CLIncidentUtilsAJAX.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	calculateDueDate: function(){
		var gd = new GlideDuration(this.getParameter('sysparm_duration'));
		var second = gd.getNumericValue() / 1000;
		var gdt = new GlideDateTime();
		gdt.addSeconds(second);
		return gdt.getValue();
	},

    type: 'CLIncidentUtilsAJAX'
});

 

Cheers,

Tai Vu

Hi@Tai Vu ,

 

Thank you, please let us know why I'm getting time different 

please see below snip you will get an idea. I have selected duration 7 hours but in due date i'm getting 22 hours . But current time is 7 hr 46 min. Please let us know If still I want to modify any thing.

Sirri_0-1701272894411.png

 

Hi @Sirri 

It's because the difference between user's timezone and the system timezone (UTC by default).

let's change the line return from the function in the script include.

From

return gdt.getValue();

To

return gdt.getDisplayValue();

 

Cheers,

Tai Vu

Sirri
Tera Guru

Hi @Tai Vu ,

 

Thank you so much.