Onchange client script for date validation

shareef1
Tera Contributor

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.

 

2 ACCEPTED SOLUTIONS

OlaN
Giga Sage
Giga Sage

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

View solution in original post

AnubhavRitolia
Mega Sage
Mega Sage

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.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

7 REPLIES 7

OlaN
Giga Sage
Giga Sage

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

Here is a good article, on getting started with Glide Ajax.

https://www.servicenow.com/community/developer-articles/client-side-scripting-go-for-glideajax-with-...

 

 

 

 

 

shareef1
Tera Contributor

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

 

 

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