Date/time validation needed done on server side. validate that minimum start date and end date 30 min if less than dont allow to form submission

vinu2
Giga Contributor

Please help me on this iam started recently learning sNow.

I have two fields StartDate and EndDate . So Date/time validation needed done on server side. validate that minimum start date and end date   30 min if less than dont allow to form submission how to write scripting for this??

Thanks in advance

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vinu,

So here is the server side script; send startDate and endDate from onSubmit client script to script include using GlideAjax

This won't work in scoped app since it doesn't support dateDiff() method

getDifference: function(){

var startTime = this.getParameter('sysparm_startDate');
var endTime = this.getParameter('sysparm_endDate');

var start = new GlideDateTime();
start.setValue(startTime);

var end = new GlideDateTime();
end.setValue(endTime);

var diff = gs.dateDiff(start,end,true);

if(diff < 1800){
gs.info("Difference is less than 30mins");
return false;
}
return true;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

View solution in original post

10 REPLIES 10

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vinu,

So here is the server side script; send startDate and endDate from onSubmit client script to script include using GlideAjax

This won't work in scoped app since it doesn't support dateDiff() method

getDifference: function(){

var startTime = this.getParameter('sysparm_startDate');
var endTime = this.getParameter('sysparm_endDate');

var start = new GlideDateTime();
start.setValue(startTime);

var end = new GlideDateTime();
end.setValue(endTime);

var diff = gs.dateDiff(start,end,true);

if(diff < 1800){
gs.info("Difference is less than 30mins");
return false;
}
return true;

}

Mark Correct if this solves your issue and also mark Helpful if you find my response worthy based on the impact.
Thanks
Ankur

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

Hi Ankur 

Thank you for your reply I have done this on Script include but in OnSubmit() is less than 30 min it dont allow to submit but it is not working

Just check it my code

function onSubmit() {
//Type appropriate comment here, and begin script below
var sdt = g_form.getValue('u_start_date');
var edt = g_form.getValue('u_end_date');
var ga = new GlideAjax('ValidateDate');
ga.addParam('sysparm_name', 'addDateTimeAmount');

ga.addParam('sysparm_end_date', edt);
ga.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
//var answer = ga.getAnswer();
if(answer == 'false')
{

alert("not available ");
return false;

}

 

Hi Vinu,

Are those date/time fields or only date field.

Also check whether log is coming or not in script include and what time is getting printed in log. is your script include client callable?

Regards

Ankur

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

Those are Date/time fields