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.

Onchange client script for date validation

Ak8977
Tera Expert

I have two date time fields "In Time" and "Out Time", the out time would be one hour from the In Time. 
I created a on change client script on In Time field. I used the following script, but it was not working.

var timein = g_form.getValue('in_time');

var timeout = g_form.getValue('out_time');

if (timeout === '') {
var timeinDate = new GlideDateTime(timein);
timeinDate.addSeconds(3600); 
g_form.setValue('time_out', timeinDate);

}

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Ak8977 

you want to populate Out time with adding 1 hour to In Time right?

if yes then you can do this with simply onChange client script and no GlideAjax required

var timein = g_form.getValue('in_time');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 1 * 60 * 60 * 1000; // 1 means 1 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('time_out', formatDate(newDT, g_user_date_time_format));

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

8 REPLIES 8

fidel_ey
Tera Guru

Hi AK8977.

Maybe this link can help you, from another question in the community.

https://www.servicenow.com/community/itsm-forum/how-to-get-difference-between-two-dates-start-date-a...

 

But in my opinion you should make some test in your script and put some alerts to show haw value is in the field. Other thing in ServiceNow :
if (timeout === '') --> Use --> if (timeout == '' ") 

The strict values not usually works in SN

Vishal Birajdar
Giga Sage

Hi @Ak8977 

 

As @Peter Bodelier  suggested you need to write onChange client script and script include to set the date after one hour.

 

Please follow below script :

 

Step 1 : Create onChange client script on "in_time" field

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if (isLoading || newValue === '') {

        return;

    }

    if (g_form.getValue('in_time')) {

        var ga = new GlideAjax("DateTimeUtil");   //script include name

        ga.addParam('sysparm_name', "getDate"); //function name

        ga.addParam('sysparm_date', g_form.getValue('in_time'));

        ga.getXMLAnswer(callBack);

    }

    function callBack(answer) {

        if (answer) {
            g_form.setValue('out_time',answer);
           
        }

 

Step 2 : Write Client Callable Script Include 

Name : DateTimeUtil

 

getDate: function() {

        var date = this.getParameter('sysparm_date');

        var gdt = new GlideDateTime(date);

        gdt.addSeconds(3600);

        return gdt;

    },

 

Hope this will help..!!

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Ankur Bawiskar
Tera Patron
Tera Patron

@Ak8977 

you want to populate Out time with adding 1 hour to In Time right?

if yes then you can do this with simply onChange client script and no GlideAjax required

var timein = g_form.getValue('in_time');
var dateMS = getDateFromFormat(timein, g_user_date_time_format);
dateMS += 1 * 60 * 60 * 1000; // 1 means 1 hour here
var newDT = new Date();
newDT.setTime(dateMS);
g_form.setValue('time_out', formatDate(newDT, g_user_date_time_format));

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

Hello @Ankur Bawiskar ,
thank you very much. The solution worked for me.