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

Tudor
Tera Guru

Hi Mattrom,

Are you looking to do this client side or server side?

Regards,

 Tudor

 

Catalog client script

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Matt,

So you are saying any value which is beyond 30 days before now should show error.

you can use UI policy with condition as start date is before 30 days from now

start date relative on or before 30 days from now

find_real_file.png

in execute if script have this script

function onCondition() {
alert('Date must not be beyond 30 days prior to now');
g_form.clearValue('start_date');

}

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 tried this but nothing happens