- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 05:39 AM
We have a requirement that checks the start date field to see if it is within 30 days prior or less.
For example:
If user selects 11/9/2019 it would fail, 11/10/2019 is fine and any date after is fine.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 06:14 AM
I was able to get it.. i dug down on an existing code and saw it went ot a script include. which i was able to find the function for getDateTimeBeforeNow and edited a script
//Checks to make sure start date inputted is not before today's date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt = g_form.getValue('start_date'); //First Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNow');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer > 30){
alert("Start date cannot be more than 30 days from todays date.");
g_form.clearValue('start_date');
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 05:52 AM
Hi Matt,
Can you share the script or screenshot here?
This works in my script. It takes care of the user date format as well
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
12-10-2019 05:58 AM
I did it within the ui policy....Start Date relative to 30 days on or before...Can you share how you do it?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-10-2019 06:07 AM
Hi Matt,
yes the condition seems good; did you try to add alert in the execute if true script to know whether it is satisfying; also add alert in execute if false script
Mark ✅ Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
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
12-10-2019 06:14 AM
I was able to get it.. i dug down on an existing code and saw it went ot a script include. which i was able to find the function for getDateTimeBeforeNow and edited a script
//Checks to make sure start date inputted is not before today's date
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cdt = g_form.getValue('start_date'); //First Date/Time field
var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeBeforeNow');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer > 30){
alert("Start date cannot be more than 30 days from todays date.");
g_form.clearValue('start_date');
}
}
}