Validate date and time on a due date field that the entered value is not in the Past

Supritha3
Tera Contributor

Hi Team,

 

I have a requirement to validate the entered date and Time is not in the past on the incident due date field.

I've tried to achieve this via Client Script and a Script Include.

 

The onChange Client script I have written on change of due date:

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
 
var ga = new GlideAjax('DateandTime');
ga.addParam('sysparm_name', 'getcurrentdate');
ga.addParam('sysparm_dueDate', newValue);
ga.getXML(UpdatePublished);
}
 
function UpdatePublished(response) {
 var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage('answer is : ' +answer);
 if (answer == "true") {
 
g_form.addErrorMessage('Please ensure that the Due date is in the future');
g_form.clearValue('due_date');
    return;
   }
else{
return;
}
 
}
 
Script Include:
 
var DateandTime = Class.create();
DateandTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getcurrentdate: function() {
var DueDate = this.getParameter('sysparm_dueDate');  
var dueDate1 = new GlideDateTime(DueDate);
var time1 = new GlideDateTime();
gs.addInfoMessage("due date formatted : " +dueDate1 +' current timeanddate: ' +time1 + "due date asis: "+" " +DueDate);
if(dueDate1 < time1){
return true;
}
else
return false;
},
 
    type: 'DateandTime'
});
 
 
while the above codes are working fine in validating the dates but are failing in validating the time.
 
Also using the below piece of code also gives time in zero's which is a wrong data.
 
var DueDate = this.getParameter('sysparm_dueDate');  
var dueDate1 = new GlideDateTime(DueDate);
 
Can someone please help me figure out the issue.
13 REPLIES 13

Nayan  Dhamane
Kilo Sage
Kilo Sage

Hello @Supritha3 ,

If you want to just validate if the fields selected due date is not of past you can do it via Ui policy only. just follow below screenshots and you will be able to achieve it.Screenshot 2023-08-22 at 1.56.41 PM.pngScreenshot 2023-08-22 at 1.56.56 PM.png

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

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hi Nayan,

 

Thank you for the suggestion, but I want this validation on change of the due date and not onload of the form. 

The user should not be allowed to chose the due date in past during the selection itself.

 

Hi @Supritha3 ,

The UI policy approach which I shared with you is also onchange it will work if the user tries to select the due date of past. And if you do not want this to work on load just uncheck the onload checkbox.

 

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

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Hi Nayan,

 

Again this only validates the date and not the time. suppose a user selects the same date and like a few hours before the current time it still allows to save.