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

@Ankur Bawiskar  - Hi Ankur,

 

Is there anything you can help on related to this issue please.

@Supritha3 

I shared solution below 4 hours ago.

Did you check that?

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

Ankur Bawiskar
Tera Patron
Tera Patron

@Supritha3 

try this client script and also make that field as mandatory.

if you don't make it mandatory the user can submit form even with wrong date

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	if(g_form.getValue('due_date') != ''){
		var nowTime = new Date().getTime();
		var dueDate = new Date(g_form.getValue('due_date')).getTime();
		if(dueDate < nowTime){
			g_form.addErrorMessage('Please ensure that the Due date is in the future');
			g_form.clearValue('due_date');
			g_form.setMandatory('due_date');
		}
	}
}

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

 

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

Hi @Ankur Bawiskar ,

 

The script you have provided is giving the below result as attached when printed.

Supritha3_0-1692712342834.png