function onChange(control, oldValue, newValue, isLoading) { if (isLoading || newValue == '') { return; } if(g_form.getValue("request_type")=="1" || g_form.getValue("request_type")=="Rehire / Re-Enable User") { if (new Date(g_form.getValue("start_date")) > new Date(g_form.getValue("end_date"))) { g_form.clearValue("end_date"); alert("End Date should not exceed more than one year from start date"); } var getDate = g_form.getValue('start_date'); //Choose the field to add time from if(g_form.getValue("end_date")!="") { var addtime = 365; //The amount of time to add var addtype = 'day'; //The time type. Can be second, minute, hour, day, week, month, year. var ajax = new GlideAjax('ClientDateTimeUtils'); ajax.addParam('sysparm_name', 'addDateTimeAmount'); ajax.addParam('sysparm_fdt', getDate); ajax.addParam('sysparm_addtime', addtime); ajax.addParam('sysparm_addtype', addtype); ajax.getXMLAnswer(doSomething); } } function doSomething(answer){ var ans=answer.split(" "); if (new Date(g_form.getValue("end_date")) > new Date(ans[0])) { g_form.clearValue("end_date"); alert("End Date should not exceed more than one year from start date"); } }
Validation to check end date should not be greater than start date+1 year ServiceNow
Working Code Edited question February 23, 2024