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.

Date before today then only submit otherwise error

shaheershaik
Kilo Contributor

Hi every one

My requirement is 

In a form i created  a  Date type variable and suppose i select any Date before today's date then only form will be submit.other wise it shows error message.

 

 

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Hi, 

Please write an onSubmit script as below, 

function onSubmit() {
  var dat = g_form.getValue('u_current_time'); //Put in the field name
  var reqDate = new Date(dat);
  var today = new Date();
   if(reqDate.getDate() > today.getDate()) {
     alert('The Date selected is in the future');
     return false; 
}
}

View solution in original post

4 REPLIES 4

Ashutosh Munot1
Kilo Patron
Kilo Patron

Hi,

 

You have to create a Onchange Client script on this Date field and use GlideAjax which will call script include and return the difference from this date field and current date time.

 

I will provide you a script in a while.

 

Thanks,
Ashutosh Munot

Ashvini Kadus1
Kilo Guru

Hey hi 

shaheershaik,

 

//Write before business rule on your table.

(function executeRule(current, previous /*null when async*/) { 

var dat = current.u_opened_at;

var gdt = new GlideDateTime();


var today = gdt.getDate();

// gs.addInfoMessage(dat);
// gs.addInfoMessage(today);

if(dat < today) {

gs.addInfoMessage('The Date selected is in the past');
current.update();


}
else
current.setAbortAction(true);

gs.addInfoMessage('ERROR!!Date is in invalide state!!');

})(current, previous);

 

this is working well on my instance.

Please mark correct if helpful.


Regards,
Ashvini.

 



Harneet Sital
Mega Sage
Mega Sage

Hi, 

Please write an onSubmit script as below, 

function onSubmit() {
  var dat = g_form.getValue('u_current_time'); //Put in the field name
  var reqDate = new Date(dat);
  var today = new Date();
   if(reqDate.getDate() > today.getDate()) {
     alert('The Date selected is in the future');
     return false; 
}
}

Thanks  Harneetsital