Interested in a ServiceNow event built for developers? Registration for now[dev]26 is officially open!

Date field should not take past date.

Sid_Takali
Kilo Patron

Hi all,

I have Date type field called "Reservation Time" and the requirement is, it should not take past date. I have created a onChange Client Script on Reservation Time field. 

Requirement :- Date field should not take past date but should take Todays date only if timing is from future. 

 

onChange  Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
var reservationDate = new Date(newValue);
var currentDate = new Date();  
if (reservationDate < currentDate )
{
g_form.addErrorMessage('Please Select Future date & time'));
g_form.clearValue('u_reservation_time_cb');
 return false;
}

 This is working as it is not taking any past date but Today's date is also not taking. 

What Can I do to achieve this use case. How do I match the timing as well. 

 

Thanks!!

1 ACCEPTED SOLUTION

Priyanka0402
Mega Sage

Hello @Sid_Takali 

Use below code:

OnChange Client Script : field name : date_time

Script :

function onChange(control, oldValue, newValue, isLoading)

{

if(isLoading){ return; }

if(newValue != ''){

var ga = new GlideAjax('CheckDate');

ga.addParam('sysparm_name', 'chkCatDate');

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

ga.getXML(DatParse);

}

function DatParse(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

if(answer == 'false'){

alert("Date cannot exist in past.");

g_form.setValue('date_time', ''); // Empty the variable.

}}}

 

___________________________________________________________________________

 

Now write a script include as follows :-

 

Name : CheckDate

Client callable : true

Script :

var CheckDate = Class.create();

CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

chkCatDate : function() {

 

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

var currDay = gs.now();

if(start < currDay){

return false;

}

else

{ return true; } } });

________________________________________________________

 

OR you could use UI Policy

Table - Table Name

Conditions - Planned Start Date before Today

 

Execute if true Scripts - 

 

function onCondition() {

    alert('Please select future Date & Time');

 g_form.clearValue('u-reservation_time_cb');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards

Priyanka 

View solution in original post

7 REPLIES 7

Priyanka0402
Mega Sage

Hello @Sid_Takali 

Use below code:

OnChange Client Script : field name : date_time

Script :

function onChange(control, oldValue, newValue, isLoading)

{

if(isLoading){ return; }

if(newValue != ''){

var ga = new GlideAjax('CheckDate');

ga.addParam('sysparm_name', 'chkCatDate');

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

ga.getXML(DatParse);

}

function DatParse(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

if(answer == 'false'){

alert("Date cannot exist in past.");

g_form.setValue('date_time', ''); // Empty the variable.

}}}

 

___________________________________________________________________________

 

Now write a script include as follows :-

 

Name : CheckDate

Client callable : true

Script :

var CheckDate = Class.create();

CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

chkCatDate : function() {

 

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

var currDay = gs.now();

if(start < currDay){

return false;

}

else

{ return true; } } });

________________________________________________________

 

OR you could use UI Policy

Table - Table Name

Conditions - Planned Start Date before Today

 

Execute if true Scripts - 

 

function onCondition() {

    alert('Please select future Date & Time');

 g_form.clearValue('u-reservation_time_cb');

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards

Priyanka 

Hii @Priyanka0402 I got issue here 

  1. when Select a date in the past (a warning message appears)
  2. when Select a future date nothing will happen as expected
  3. when Select a past date again, clearing date field but (no warning message appears). 

so, what do you think? why its not working?

Hello @Sid_Takali 

 

I tried it in my Instance, it is working fine. I would suggest you to please check the code once.

 

Thank you.

Ranjit Nimbalka
Mega Sage

Hi @Sid_Takali,

Restricting dates can also be done almost no code (only code would be the error message) Using UI Policy.

 

If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.

Regards,

Ranjit