Start date and end date validation

aamir1
Mega Expert

Hi,

I am new to service-now and facing some issues.

I have two date field variables in my catalog item and i need to do some validations. I tried but not able to achieve this.

My requirement is -

a) Start date should be less than the end date and start date should not be less than current date. If it happens then throw error.

b) End date should not be less than current date and start date, If it happens then throw error.

Please help with examples.

Thanks

1 ACCEPTED SOLUTION

shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Ok try this simple script:



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


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


          return;


    }




      var startDate = g_form.getValue("start_date");


      var endDate = g_form.getValue("end_date");


      var format = g_user_date_time_format;



      var startDateMs = getDateFromFormat(startDate, format);


var endDateMs = getDateFromFormat(endDate, format);


//alert("Hi "+startDate);


//alert("end" + endDate);


if (startDateMs > endDateMs ) {


              g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));




              return false;




      }


}



You should run this script on end date check this screenshot:


Screen Shot 2017-10-11 at 11.51.46 PM.png



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


View solution in original post

8 REPLIES 8

Jaspal Singh
Mega Patron
Mega Patron

Hi Aamir,



You can have an OnSubmit Catalog client script written for Disable the Change of Planned Start & End date field validated.



Link function onSubmit - Client Script Help   should help you script.


Hi Jaspal,



I want to do it on   onchange. I tried with conditions like start date < end date etc etc. but not getting proper result.


shruti_tyagi
ServiceNow Employee
ServiceNow Employee

Hi Aamir,



There is OOB client script 'Planned Start/End Date Validation' in which we almost do similar kind of thing on change request form for planned start and end date. You should be able to use similar script for your requirement in onChange script:



Here is OOB script, you can use similar script I guess for your requirement:



=====Start=====


var startDate = g_form.getValue("start_date");


      var endDate = g_form.getValue("end_date");


      var format = g_user_date_time_format;




      if (startDate === "" || endDate === "")


              return true;



      // get date strings into a number of milliseconds since 1970-01-01


      var startDateMs = getDateFromFormat(startDate, format);


var endDateMs = getDateFromFormat(endDate, format);



if (startDateMs < endDateMs)


return true;



g_form.clearMessages();



// 0 from "getDateFormat" means an invalid date string was passed to it


if (startDateMs === 0 || endDateMs === 0) {


              if (startDate === 0)


                      g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start_date")));




              if (endDate === 0)


                      g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("end_date")));




              return false;


}



if (startDateMs > endDateMs)


              g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("end_date"), g_form.getLabelOf("start_date")));




              return false;



======END=======



Thanks


Shruti


If the reply was informational, please like, mark as helpful or mark as correct!


Hi Shruti,



Thanks for sharing this.



I tried your code with modifications but not happening anything. I tried to get alert for startDateMs but its not returning anything. Below is the code-



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


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


          return;


    }




      var startDate = g_form.getValue("newuser_startdate");


      var endDate = g_form.getValue("newuser_enddate");


      var format = g_user_date_format;



      if (startDate === "" || endDate === "")


              return true;



      // get date strings into a number of milliseconds since 1970-01-01


      var startDateMs = getDateFromFormat(startDate, format);


      var endDateMs = getDateFromFormat(endDate, format);


//alert("Hi "+startDateMs);



      if (startDateMs < endDateMs)


        return true;



      g_form.clearMessages();



// 0 from "getDateFormat" means an invalid date string was passed to it


if (startDateMs === 0 || endDateMs === 0) {


              if (startDate === 0)


                      g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("newuser_startdate")));




              if (endDate === 0)


                      g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("newuser_enddate")));




              return false;


}



if (startDateMs > endDateMs)


              g_form.addErrorMessage(new GwtMessage().getMessage("{0} must be after {1}", g_form.getLabelOf("newuser_enddate"), g_form.getLabelOf("newuser_startdate")));



              return false;


   


}