How to restrict past date selection.

devr
Kilo Contributor

Hi All,

I found below details from one of the thread to restrict past date selection in catalog item however the same is not working in Normal table form, please suggest how to achieve the same in normal table forms (reference- https://community.servicenow.com/thread/159148😞

____________________________________________________________________________

OnChange Client Script : field name : date_time
Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading){ return; }
if(newValue != ''){
var ga = new GlideAjax('CheckDate');
ga.addParam('sysparm_name', 'chkCatDate');
ga.addParam('sysparm_date',g_form.getValue('date_time'));
ga.getXML(DatParse);
}
function DatParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date cannot exist in past.");
g_form.setValue('date_time', ''); // Empty the variable.
}}}

___________________________________________________________________________

Now write a script include as follows :-

Name : CheckDate
Client callable : true
Script :
var CheckDate = Class.create();
CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatDate : function() {

var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });

1 ACCEPTED SOLUTION

Prateek kumar
Mega Sage

Hello


Try this and let me know if i can help you with anything.


Script Include:


Name:DateValidation


Client callable: Checked



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate: function() {


  var ActualEndDate = this.getParameter('sysparm_end_date');


  return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;


  },


      type: 'DateValidation'


});



Client Script:



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


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


              return;


      }


      var ga = new GlideAjax('DateValidation');


      ga.addParam('sysparm_name','validateDate');


      ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field


      ga.getXML(ProcessResult);


     


     


      function ProcessResult(response) {


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


              if (answer < 0)


                      {


                      g_form.clearValue('u_business_need_by_date'); //give your date field


                      alert('Business need date should not be in the Past.');


                     


              }


      }


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

View solution in original post

12 REPLIES 12

devr
Kilo Contributor

Hi Harel,



I tried as you suggested but its not working:



Client Script: (field name is u_friday_review_date)



function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
          return;
    }


    //Type appropriate comment here, and begin script below
        var ga = new GlideAjax("NowDateCalc");
ga.addParam("sysparm_name", "getNow");
ga.getXML(getDate);
}
function getDate(response) {
var my_date = g_form.getValue('u_friday_review_date');
var rightNow = response.responseXML.documentElement.getAttribute("answer");
if (my_date < rightNow) {
  alert("Please select future Friday Review Date.");
  g_form.setValue('date_needed', '');
}
}



________________________________________________________________________________________



Script Include:


var RestrictingPastDate = Class.create();
RestrictingPastDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getNow : function() {
  return gs.now();
},


      type: 'RestrictingPastDate'
});



please let me know if I am missing anything in above scripts.


fyi: we are on Jakarta



Thank You


Prateek kumar
Mega Sage

Hello


Try this and let me know if i can help you with anything.


Script Include:


Name:DateValidation


Client callable: Checked



var DateValidation = Class.create();


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  validateDate: function() {


  var ActualEndDate = this.getParameter('sysparm_end_date');


  return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;


  },


      type: 'DateValidation'


});



Client Script:



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


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


              return;


      }


      var ga = new GlideAjax('DateValidation');


      ga.addParam('sysparm_name','validateDate');


      ga.addParam('sysparm_end_date',g_form.getValue('u_business_need_by_date'));//give your date field


      ga.getXML(ProcessResult);


     


     


      function ProcessResult(response) {


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


              if (answer < 0)


                      {


                      g_form.clearValue('u_business_need_by_date'); //give your date field


                      alert('Business need date should not be in the Past.');


                     


              }


      }


}



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

Thank You Prateek !!! it worked.


Hi Prateek, I just recently started learning service now. I came across this question saw your solution. I have a doubt. What is this mean -----return gs.dateDiff(gs.now(),ActualEndDate, true)/86400;------ I mean why did you write 86400, I am curious to know why and what it will do. please explain Thanks

gs.now() -- Gives you current date

ActualEndDate -  is the value end user has picked

gs.dateDiff(gs.now(),ActualEndDate, true)-- will print you the difference between these two dates in seconds 

If you divide these seconds with 86400, you'll get the result in days.(24hrs*60mins*60Seconds)


Please mark my response as correct and helpful if it helped solved your question.
-Thanks