Catalog Item: Add 7 Days Restriction

cfrazer
Giga Contributor

Hello,

On my ServiceNow catalog items, I have a field asking users when they would ideally need to receive their item by (it is a date field called needed_by). I would like to set a restriction that they cannot pick a date unless it is 7 days from the current date. If they pick a date sooner, I want a message to pop up stating that they need to pick a different date. I have tried everything, but cannot get this to work. Any help is appreciate. Here is my code so far:

function onChange(control, oldValue, newValue, isLoading) {  
 
if(isLoading || newValue == '') {  
return;  
}  
var date = now().adddays(7);

var select= new Date(getDateFromFormat(g_form.getValue('needed_by'),g_user_date_format));

if (select < date) {  
alert('Please select a future date.');  
g_form.clearValue('needed_by');  
}  
 
}  

5 REPLIES 5

Jon S_
Giga Contributor

Untested as I modified my script to match yours:



function onChange(control, oldValue, newValue, isLoading) {


if (isLoading) {


return;


}


if (newValue != '') {


g_form.hideFieldMsg('needed_by', true); //clear the message when they choose something else


var dayDifference = 0;


var dateNow = new Date();


var requestedDate = Date.parse(newValue);


dayDifference = (requestedDate - dateNow.getTime())/60/60/24/1000;



if (dayDifference < 7) {


g_form.hideFieldMsg('needed_by', false);


g_form.showFieldMsg('needed_by','Please select a future date.','error');


g_form.setValue('needed_by', '');


return false;


}


}


}


SanjivMeher
Kilo Patron
Kilo Patron

You need a client script and a script inlucde.



function onChange(control, oldValue, newValue, isLoading) {


g_form.hideFieldMsg('requested_by_date', true);




if (isLoading || newValue == '') {


return;


}



var startdate = g_form.getValue('needed_by'); //Choose the field to add time from




var ajax = new GlideAjax('DateValidation');


ajax.addParam('sysparm_name', 'ValidateStartDate');


ajax.addParam('startdate', startdate);


ajax.getXML(validateDate);


//Type appropriate comment here, and begin script below


}




function validateDate(response){


var answer = response.responseXML.documentElement.getAttribute("answer");



if (answer==2)


g_form.showFieldMsg('requested_by_date',"Please consider at least 1 weeks window for work completion",'error');


else if (answer==1)


g_form.showFieldMsg('requested_by_date',"The start date can't be in past",'error');


}






Use below script include function. Create a new script include with name 'DateValidation' and make it client callable. Copy and paste below script inside the script include






ValidateStartDate: function() {


var se_start_date = this.getParameter('startdate');



var opened_date = gs.now();


var currentDateTime = new GlideDateTime();


currentDateTime.setDisplayValue(opened_date,"MMM d, yyyy");


var start_date = new GlideDateTime();


start_date.setDisplayValue(se_start_date,"MMM d, yyyy");



if (se_start_date!='' && start_date<currentDateTime)


{


return 1;


}


else if (se_start_date!='')


{


var dc = new DurationCalculator();



dc.setStartDateTime(currentDateTime);



if (!dc.calcDuration(7*24*3600))                       // Add 1 weeks


gs.log("*** Error calculating duration");



var newDateTime = dc.getEndDateTime();


if (start_date < newDateTime)


{


return 2;


}


}


},



Please mark this response as correct or helpful if it assisted you with your question.

Hi, thank you for your help.


find_real_file.png


How would I fix this?