- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2021 09:02 AM
Hi! - I have a date field variable named change_date in a catalog item. I want to send the user an error message if the date they enter is less than the current date plus 3 days - I presume I need to do this in a catalog client script but unsure of the logic to use - Thanks!
Solved! Go to Solution.
- Labels:
-
Service Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2021 06:48 AM
In the Execute if true script do this
1) clear the variable and make it mandatory
2) This will ensure the user gives valid date before submission
g_form.clearValue('variableName'); // your variable name here
g_form.setMandatory('variableName', true); // your variable name here
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-02-2021 09:38 AM
Hi,
You need to use onChange Catalog client script for this and make a glide ajax call to perform date validation.
Your client script would be like:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cdt = g_form.getValue('change_date');
var ajax = new GlideAjax('ClientDateUtils');
ajax.addParam('sysparm_name', 'getDateDiffFromCurrent');
ajax.addParam('sysparm_dt', cdt);
ajax.getXML(doSomething);
function doSomething(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer < 0) {
getMessage('Change Date must be after 3 days from today\'s date', function(msg) {
alert(msg);
});
g_form.setValue("change_date", "");
}
}
}
and your script include function in client callable script include would be like:
getDateDiffFromCurrent: function(){
var date_time =gs.nowDateTime();
var gdt =new GlideDateTime();
gdt.setDisplayValue(date_time );
gdt.addDaysLocalTime(3);
var firstDate = gdt.getLocalDate();
var secondDT = this.getParameter('sysparm_dt');
var secondDate = secondDT.split(' ')[0];
var diff = gs.dateDiff(firstDate, secondDate, true);
var timediff = this._calcDateDiff(diffTYPE, diff);
return timediff;
},
For more learning you can refer below link:
https://community.servicenow.com/community?id=community_blog&sys_id=467c62e1dbd0dbc01dcaf3231f9619ad
Thanks,
Anil Lande
Thanks
Anil Lande

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2021 08:22 AM
Hi,
Use below script in onchange client script of the catalog,
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '')
g_form.hideFieldMsg('sh_request_untill');
var gdt = new Date(newValue);
var curDate = new Date(new Date().toISOString().split('T')[0]) ;
//Validates the date cannot be set further than 180 days in the future
curDate.setDate(curDate.getDate() + 3);
if(gdt<curDate) {
g_form.showFieldMsg('sh_request_untill', getMessage('Please choose a valid date. Dates cannot be less than 3 days to the present date'), 'error');
return false;
}
return true;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-03-2021 08:30 AM
Thanks Community, all answers very helpful!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-04-2021 01:53 AM
Hi,
1. Create one Catalog onChange Client script(apply on change_date variable). And Create one Script Include(make it client callable).
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('Demo4JunAjax'); // script include name
ga.addParam('sysparm_name', 'getCurrentDate'); // function name
ga.addParam('sysparm_date', newValue); //value which need to send server
ga.getXML(callBackFun); // call back function
function callBackFun(response) {
var answe = response.responseXML.documentElement.getAttribute('answer');
}
}
var Demo4JunAjax = Class.create();
Demo4JunAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getCurrentDate: function() {
var enteredDate = this.getParameter('sysparm_date');
var gd = new GlideDateTime();
gd.addDaysLocalTime(3);
var currentDate = gd.getDate();
if(enteredDate < currentDate){
gs.addErrorMessage('Enter Valid Date');
}
},
type: 'Demo4JunAjax'
});