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

I cleaned up the script provided by Janjiv:


Be sure to create your Script include with the following details:


Name: DateValidation


Client callable: true


Accessible from: All application scopes


Script:


var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



      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;


                      }


              }


      },



      type: 'DateValidation'


});