- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 02:54 AM
Hi All,
I am trying to write an onChange client script to validate date fields. I have 4 date filed's like A-0, A-15, A-30, A-60.
These are populating automatically when the form is loading. but now i want to change one field that is A-0. Based on this remaining fields will calculate.
I mean when i change A-0 field,current date to another date i want to calculate reaming fields and populate the fields.
This is my requirement. can any one help me how to write a OnChange client script for this.
Regards,
Shareef.
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 03:35 AM
Hi,
There are multiple ways to solve this, either you recalculate the other date directly in a client script.
Or you can use a GlideAjax and a Script include. If going for the second option you pass the start date into the function, do the calculations in the script include, and return an object with the recalculated dates to be used in the client script to set new values.
A simple client script can set new values as this:
var startDate = g_form.getValue('some_date_time_field');
var gwtDate = new GwtDate(startDate);
gwtDate.addSeconds(3600); // this adds an hour to the starting date
g_form.setValue('some_other_field_name', gwtDate); // set the recalculated date in another field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 04:17 AM
Hi @shareef1
You can find the code below:
Client Script:
var ga = new GlideAjax('ABCDateUtils'); // or 'global.ABCDateUtils' if Script include is in Global scope and Client script is in Another scope
ga.addParam('sysparm_name', 'setGoLiveDate');
ga.addParam('sysparm_date', g_form.getValue('u_a_0')); // u_a_o is date field which may vary based the field name on your instance
ga.getXML(SetDate);
function SetDate(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.setValue('u_a_15',answer); // set value of other date field
.....
}
Script Include:
var ABCDateUtils = Class.create();
ABCDateUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
setGoLiveDate: function() {
var date = this.getParameter('sysparm_date');
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(days);
return gdt.getLocalDate();
},
type: 'ABCDateUtils'
});
NOTE: If you are creating new Script include, prefer to creating in same application scope to avoid any access restriction issues/conflicts.
Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 03:35 AM
Hi,
There are multiple ways to solve this, either you recalculate the other date directly in a client script.
Or you can use a GlideAjax and a Script include. If going for the second option you pass the start date into the function, do the calculations in the script include, and return an object with the recalculated dates to be used in the client script to set new values.
A simple client script can set new values as this:
var startDate = g_form.getValue('some_date_time_field');
var gwtDate = new GwtDate(startDate);
gwtDate.addSeconds(3600); // this adds an hour to the starting date
g_form.setValue('some_other_field_name', gwtDate); // set the recalculated date in another field

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 03:42 AM
Here is a good article, on getting started with Glide Ajax.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 03:46 AM
Hi OlaN,
Yeah I have written script include for this.
This is my script include:
var ABCDateUtils = Class.create();
ABCDateUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
setGoLiveDate: function(days) {
var gdt = new GlideDateTime();
gdt.addDaysLocalTime(days);
return gdt.getLocalDate();
},
type: 'ABCDateUtils'
});
Now i want to write Onchange client script for this by using glide ajax. how can i archive.
Please help me.
With regards,
Shareef

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-15-2022 03:58 AM
Please read the article I've linked above.
Basically you want to write something like this in your client script:
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionNameInScriptInclude');
ga.addParam('sysparm_date', newValue); // the date to calculate
ga.getXMLAnswer(validate);
function validate(response)
{
if (response != '')
{
g_form.setValue('field_name', response);
}
}
And in your script include, you need to retrieve the date parameter by adding a line like this:
var inputDate = this.getParameter('sysparm_date');