how to prevent user to select past date and time of all date fields in change form.

venkysana
Tera Expert

User needs to be restricted when user selects the past time in the date fields in the change form. Need to create BR / client script against The Planned Start and End times along with the Outage Start and End times. The business rule/ Client script should prevent all ITIL users from setting a date and time in the past based on their current system time. 

I have created Below script include and client script. It is working on preventing user if he selects previous date. But I want to restrict user to select past time also. Please give me solution.

Script Include:

find_real_file.png

Client Script:

I have created OnChange Client Script on Planned Start date field as below.

find_real_file.png 

1 ACCEPTED SOLUTION

Gaurav Shirsat
Mega Sage

Hello 

Please use my below working Scripts, I have achieved this in my PDI. You only need to replace your variables.

onChange Catalog Client Script:-

1) Client Script for Future Date Validation:-

find_real_file.png

2) Past Date Validation:-

find_real_file.png

3) Common Client Callable Script Include:- 

find_real_file.png

4) Script Include Code;-

find_real_file.png

Please Mark Correct and Helpful
Thanks and Regards
Gaurav Shirsat

View solution in original post

16 REPLIES 16

Anil Lande
Kilo Patron

Hi,

Please check below Link.

https://community.servicenow.com/community?id=community_question&sys_id=af9e8417db939780a39a0b55ca96...

 

Thanks,
Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande

Maik Skoddow
Tera Patron
Tera Patron

Hi @venkysana ,

the following script iterates over all fields of the current record and filters out all of the type 'glide_date' and 'glide_date_time'. Then it checks whether its value is less than the current timestamp (compared on the basis of milliseconds).

var numNowMillis = new GlideDateTime().getNumericValue();

for (var x in current) {
  if (x.substr(0,3) != 'sys') {
    var internalType = current.getElement(x).getED().getInternalType();

    if (internalType.substr(0, 10) == 'glide_date') {
      var numMillis = current[x].getGlideObject().getNumericValue();

      if (numMillis > 0 && numMillis < numNowMillis) {
        //insert error message here
      }
    }
  }
}

Kind regards
Maik

If my answer replied your question please mark appropriate response as correct so that the question will appear as resolved for other users who may have a similar question in the future.

Thanks for your response. But where can I use this code like BR or Script include. I have used your script in BR but its not executed as expected.

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020-2021 ServiceNow Community MVP
2020-2201 ServiceNow Developer MVP

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn