How to check the start date that is 30 days prior

mattrom1
Giga Expert

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. 

1 ACCEPTED SOLUTION

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');
}

}
}

View solution in original post

8 REPLIES 8

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

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

I did it within the ui policy....Start Date relative to 30 days on or before...Can you share how you do it? 

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

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

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');
}

}
}