Selected date should be current or onwards. If previous date is selected, it will show an error msg.

MelvinBulan
Tera Contributor

Hello everyone,

 

The requirement is for the user to not be able to select a previous date, if they select a date not of the current date or onwards. It will show a red message saying "Please Select Current Date or On wards".

 

MelvinBulan_0-1669539707863.png

 

Is it possible to do using ajax or client scripts if so how should it be done?

 

 

1 ACCEPTED SOLUTION

Ishaan Shoor
Mega Sage
Mega Sage

Hi @MelvinBulan ,

Please try adding the below script include and the client script, update the name for your "date_variable" in the client script, and see how you go.

Script Include: 

 

 

var checkDateUtils = Class.create();
checkDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	dateDiff: function() {
		var firstDate = this.getParameter('sysparm_first_date');
		var secondDate = this.getParameter('sysparm_second_date') || gs.now();
		
		return gs.dateDiff(secondDate, firstDate, true);
	},

    type: 'checkDateUtils'
});

 

 

Client Script - onChange date variable

 

 

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

	// Start Date cannot be backdated
	var dateAjax = new GlideAjax('checkDateUtils');
	dateAjax.addParam('sysparm_name', 'dateDiff');
	dateAjax.addParam('sysparm_first_date', newValue);
	dateAjax.getXMLAnswer(processResponse);
}

function processResponse(answer) {
	// If Start Date is in the past (Less than 0) show an error and clear the value
	if(answer < 0) {
		g_form.clearValue('date_variable');
		g_form.showErrorBox('date_variable', 'Date cannot be in the past!');
	} else {
		g_form.hideErrorBox('date_variable');
	}
}

 

 
Hope this helps! 

If my answer helped you in any way or if this resolved your issue, please then mark it as helpful or correct accordingly to set this thread to solved.

BR.

Hope this helps!
BR.
Ishaan Shoor

View solution in original post

3 REPLIES 3

Shakeel Shaik
Giga Sage
Giga Sage

Hi @MelvinBulan 

 

Please refer to the below link.

Thanks,
Shakeel Shaik 🙂

Ishaan Shoor
Mega Sage
Mega Sage

Hi @MelvinBulan ,

Please try adding the below script include and the client script, update the name for your "date_variable" in the client script, and see how you go.

Script Include: 

 

 

var checkDateUtils = Class.create();
checkDateUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	dateDiff: function() {
		var firstDate = this.getParameter('sysparm_first_date');
		var secondDate = this.getParameter('sysparm_second_date') || gs.now();
		
		return gs.dateDiff(secondDate, firstDate, true);
	},

    type: 'checkDateUtils'
});

 

 

Client Script - onChange date variable

 

 

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

	// Start Date cannot be backdated
	var dateAjax = new GlideAjax('checkDateUtils');
	dateAjax.addParam('sysparm_name', 'dateDiff');
	dateAjax.addParam('sysparm_first_date', newValue);
	dateAjax.getXMLAnswer(processResponse);
}

function processResponse(answer) {
	// If Start Date is in the past (Less than 0) show an error and clear the value
	if(answer < 0) {
		g_form.clearValue('date_variable');
		g_form.showErrorBox('date_variable', 'Date cannot be in the past!');
	} else {
		g_form.hideErrorBox('date_variable');
	}
}

 

 
Hope this helps! 

If my answer helped you in any way or if this resolved your issue, please then mark it as helpful or correct accordingly to set this thread to solved.

BR.

Hope this helps!
BR.
Ishaan Shoor

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You do not require to write any script. You can achieve this using UI policy

 

Just write the condition as below:-

 

Saurav11_2-1669541426261.png

 

 

Saurav11_1-1669541409355.png

 

Please check the below article for the same for more detail:-

 

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

 

Please mark my answer as correct based on Impact.