Begin Date to End Date duration cannot exceed one year

Cupcake
Mega Guru

I have a need to make sure that the duration between the Begin Date and End Date does not exceed 1 year.

I used scripting before to make a date go out to xxx amount of days, but never to actually do a calculation between Begin and End dates

I don't have a Due Date field. I just have the Start and End Date field and I need to make sure that Duration cannot exceed 1 year.

1 ACCEPTED SOLUTION

Michael Ritchie
ServiceNow Employee
ServiceNow Employee

Hello Karen, its been a while hope you are well!



Here is some example code I have used before.   Do you want the validation to happen on submit or as the user is inputting the data?   My code below is based on an onChange of the Work End date on a task, but you could change it to an onSubmit can calculate and warn then.



Create a onChange client script for the End date:


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


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


              return;


      }



      var starts = g_form.getValue('work_start');


      startMillis = getDateFromFormat(starts, g_user_date_time_format);


     


      var ends = newValue;


      endMillis = getDateFromFormat(ends, g_user_date_time_format);


     


      var minutes = (endMillis - startMillis) / (1000 * 60);


     


      if(minutes > 480) {


              alert('An Activity may only last 8 hours.   You must enter multiple activities if the time required exceeds 8 hours.');


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


      }


}



Obviously you will need to calculate the a year to then compare for your alert.   Thinking out loud an onSubmit may be better because you can prevent the save if it exceeds a year.


View solution in original post

27 REPLIES 27

So full disclosure, I am on Helsinki Patch 4 and this code is working for me.   I even set my variable names to be the same as yours:


find_real_file.png



Here is my client script:


find_real_file.png



Here is a test setting start to be today's date and end to 9-24-2017:


find_real_file.png


Hey Mike look at your date for Work end. Your date says 2016-09-24 not 2017.


Sorry that was left in from a test to make sure it accepted a date within a year.   As soon as I clicked OK, the date was cleared out based on the code.


Thanks for replying Mike - still trying to find out the issue on my end.


I did notice that your 00:00:00 had double quotes around it and not single quotes like i had.


I changed that but it still did not work.


We just upgraded to Helsinki original version no patches yet for us. Not sure if that is the issue. We seem to have found alot of script issues when we initially upgraded from Eureka.



Thanks I will let you know as soon as I can get it working.



Karen


Good morning Mike,


        I am sorry to just reply to you. Been extremely busy remediating issues with the upgrade. I have just now gotten back to this. I added the alerts as you suggested and this is what I get. After I click okay on the endMillis alert I never get the message that duration cannot exceed 365 Days. So does this mean the calculation piece is not working. Is that why it is not throwing the Duration alert message.


find_real_file.png                   find_real_file.png


Thanks,


Karen