The Now Platform® Washington DC release is live. Watch now!

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Mark Roethof
Tera Patron
Tera Patron

Hi there,

Client Side date validations, o man, they can be a real pain in the but! Getting the right format, different language settings, timezones, etc.. And then working with Scoped Apps… some GlideDate functions not being available. If you look at the community posts, a lot of situations like these are posted. Scripting not working, applying GlideAjax, splitting dates, etc.. So to share some gained knowledge and experience:

There's a much easier way!

Using the No Code condition builder on (Catalog) UI Policies! Only Low Code needed for an info/error message to display to the user, maybe clearing the field/variable value, etc..

Examples

How to setup checking if…

An entered date for a Variable is in the past. If so, clear the variable value and give the User an error message.

find_real_file.png

find_real_file.png

find_real_file.png

An entered date for a Field is after 7 days in the future. If so, clear the field value and give the User a field message.

find_real_file.png

find_real_file.png

find_real_file.png

Comparing two entered dates with each other. Date 2 should not be before Date 1 (image, planned end date should not be before planned start date)

find_real_file.png

As you can see in the examples above, Low Code for the follow-up on your validation and the actual validating simply through the condition builder within a (Catalog) UI Policy.

---

And that's it actually. Not much to it. Hope you like it. If any questions or remarks, let me know!

đź‘Ť
If this post helped you in any way, I would appreciate it if you hit bookmark or mark it as helpful.

Interested in more articles, blogs, videos, and Share projects on No-code/Low-code I published?
No-code/Low-code


Kind regards,
Mark

---

LinkedIn

Comments
Lukasz Bojara
Tera Guru

Nice article, it's always good to see other people ideas on resolving common issues. Your solution is really helpful.

HugoFirst
Tera Guru

I really like this solution to an age old problem, and I'm going to start using it.
One additional thought is that the date could be edited in a list view.  In this case, the UI Policy Action could clear the value and post the error when loading the form, but that could be an unsatisfying experience for the end user ( who may not be the one who set the incorrect value ).  

I  recommend that we turn off list edits to any field validated by a client script or UI Policy.

Here's an community post on how to do that:

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

 

Steven Chaparro
ServiceNow Employee
ServiceNow Employee

Hello! 

I tried this suggestion, but using the Catalog UI Policy is not working for me on the portal. Even after selecting Today or a future day it appears the error message on top.


find_real_file.png

 

find_real_file.png

 

Here is the Portal, no matter the date I select

find_real_file.png

 

In the platform I dont have that problem when trying it from the Service Catalog. Any suggestion?

Vivek Verma
Kilo Sage
Kilo Sage

Hi Mark,

Thank you for the Article, Bookmarked is sure thing.

I have one query that it is possible to exclude holidays too.

 

Thanks,

Vivek Verma

Mark Roethof
Tera Patron
Tera Patron

Hi Vivek,

With no code and UI Policies only, this will not be possible. With GlideSchedule you could. I am already writing an article on this, so should be out soon 🙂

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

Vivek Verma
Kilo Sage
Kilo Sage

Hi Mark,
firstly best of luck for the knowledge 2020.

And I have tried to achieve the requirement by using the Glide Schedule.
Here is it,
I have managed my requirement my this code.
Maybe this will help you.

Thanks, 

Vivek

 var gSCH = new GlideRecord('cmn_schedule');
        gSCH.addQuery('type', 'test');
        gSCH.query();
        if (gSCH.next()) {
            var sched = new GlideSchedule(gSCH.sys_id);
            var d = new GlideDateTime(selectedDate);// Pass date time here
			var gdt = new GlideDateTime();
			if(d < gdt){
				return false;
			}
            if (sched.isInSchedule(d))
            return true;
            else
                return false;
        
    }},
Harish27
Kilo Guru

Thanks for the article, Mark!!.

 

This saved me a hell lot of time... 🙂

Harish27
Kilo Guru

Hi Steven,

 

You have to uncheck the On Load CheckBox

 


Please Mark âś… Correct if this solves your issue and also mark đź‘Ť Helpful if you find my response worthy based on the impact.
Thanks!
Harish

samadam
Giga Guru

Hi Steve, Were you able to resolve this issue? I have the same problem with portal

Harish27
Kilo Guru

Hi Samadam,

Please uncheck the onLoad check box and try.

Pls share your error screen shot and UI policy.

 

Thanks,

Harish M

 

ah16
Mega Expert

Hi Mark,

I'm trying to do hours validation in Duration Field on Service Portal. Any suggestions?

 

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

 

Thanks,

Ashraf

matt_ellis
Tera Contributor

Hello,

 

I'm having the same issue. The UI policy runs with every date we try to enter, even future dates. The error pops up and the variable clears, as the script should. But this is happening on every date, not just dates before the current date.

Unchecked the onLoad check box and it's still occurring.

Any thoughts @Mark Roethof ?

Joseph Armenti
Tera Contributor

Hello, 

Can I restrict a Planned Start Date for Change Requests using this????

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Not sure what your question is here or how it relates to this article. Can you explain?

Or if it's more extensive question, just create a new topic and explain your question in detail.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

Joseph Armenti
Tera Contributor

When a user is creating a Change Request. I want the user to NOT be able to enter a planned start date unless its 5 days from that day. In other words cannot select a planned start date for same day etc.

Joseph Armenti
Tera Contributor

Its related, date validation, except for a Change Request

Mark Roethof
Tera Patron
Tera Patron

Can you share what you've tried and what's not working?

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Joseph Armenti
Tera Contributor

Hello Mark, 

 

I was able to get it to work. User cannot select a Planned Start Date unless its five days in the future. Anyway to just use business days?

find_real_file.png

Brandon R1
Tera Contributor

Hi @Mark Roethof  This article is extremely helpful, and its good to know there are others who believe in low or no code solutions. I'm a huge low or no code advocate mainly because I am not a heavy coder by any means.

Your solutions partially work for me but I need to see if you can help me further. I added a UI action to make the data field Mandatory if the entered date is in the future, but I'm still allowed to proceed with the update. If the entered date is in the future, I need to prevent the user from ordering until a date is entered in that specific date field that is prior to today.

The date field I'm working with already has a date in it which is pulled from the field it's referencing in the User Profile. I'm enhancing an existing catalog item to allow users to update that date field to a date of their choosing. But, the updated date CANNOT be a future date.

Do you know how I can accomplish this??

Thanks,

Brandon

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Indeed this does not force a onSubmit validation or something. Just like Client Script date validations, you have to think of this yourself.

Some ideas:

- Clear the date field and present the error message, if the date field is mandatory the submit validation will kick in;
- (Service Portal) Add a hidden checkbox, which is set to true when the date validation kicks in, onSubmit check the value of the checkbox, if true return;
- (Platform UI) Use a scratchpad variable to set a bollean, which is set to true when the date validation kicks in, onSubmit check the value of the scratchpad, if true return.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

Tarish
Kilo Contributor

it shows the error on the try it phase for the item. But these changes do not reflect in the service portal(sp). did you find the solution to it?

 

mark_oldroyd
Tera Contributor

Hi,

I can only seem to get this to work with Date\Time fields. If I use just Date field the UI policy seems to be ignored. Does this sound correct? Or has anyone got this to work with a Date field?

Thanks,

Mark

Mark Roethof
Tera Patron
Tera Patron

Hi there,

I known there where some exceptions for Date variables in the past. Though just testing now on my Quebec instance, comparing if End Date is at or after Start Date, works fine. 

find_real_file.png

find_real_file.png

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

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

---

LinkedIn
Community article, blog, video list

mark_oldroyd
Tera Contributor

Hi Mark,

Thanks for coming back so quickly. I did sort of manage to get it to behave as expected using logic above. However, it only seemed to fire once, when I initially entered a start date the message appears. When I enter an Expiry date it goes away regardless of the date set.

I'm on Paris instance. I included screen grab of the poicy in case there's anything glaring.

Else, I can always use an onchange or on submit script.

 

Sajith3
Giga Contributor

I was asking myself why didn't I see this article earlier. It was so helpful and easy.

 

thanks

Sajith

vgani
Giga Expert

Hi Mark,

Me too facing issue, not getting expected output. In Quebec version.

find_real_file.png

find_real_file.png

Rohit70
Tera Contributor

Could you please help me with UI policy which can throw error if we select past date and date beyond 180 days...

Rohit70
Tera Contributor

Can anyone please help me with UI policy which can throw error if we select past date and date beyond 180 days...

Mark Roethof
Tera Patron
Tera Patron

Can you share what you tried and what's not working?

Kind regards,
Mark

Safaa EL Moidn1
Giga Contributor

Hi Mark, 

Your solution is very helpful, It's working fine.

Thank's

Lakshmi46
Kilo Contributor

How can I disable the past dates so that user cannot pick them?

Vijayalakshmi P
Tera Guru

Hi Mark, Will it work for multi-user environment where users are spread across globe? They work in different time zones.

Thanks.

Nellore Raveend
Tera Contributor

Hi @Mark Roethof ,

I have User delegation form on service portal , I had created an Ui Policy for date validations . It is working fine on Table form view but it is not working on Service Portal form . Can you please suggest how can we solve this issue 

 

 

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Can you share details?

Kind regards,
Mark

Nellore Raveend
Tera Contributor

Hello @Mark Roethof ,

Below mentioned data explains about my issue  ,

I am having User Delegation form on Service Portal , Whenever we selects end date is more than 30 days from start date , It should show error message and we need to cancel the form submission as well .

For achieving this we have before business rule available , it is working as expected , But whenever we are clicking on save button to submit that form then it is showing error message but after few secs it redirects to blank page .

My requirement is after showing the error message and it should be in the same page , I don't want to redirect to blank page .

Experts please suggest what changes do i need to apply in script ?

For more details i am attaching the screen shots .

Business Rule :

image

image

 

Delegate Form: Selected ends date as more than 30 days

image

After Submitting the form getting error message and within few secs redirects to blank page .

image

redirected to blank page : 

image

 

Nellore Raveend
Tera Contributor

Hi Mark, 

 

The below UI policy i have created for date validations , If the ends date is more than 30 days from start date then we need to cancel form submission .

It is working on the table form view not in Service Portal form View .

 

For your reference i have attached some screen shots , Please check and let me know what changes do i need to update here .

 

 

Mark Roethof
Tera Patron
Tera Patron

So where are you aborting to submission? Don't see that you added anything for this for the portal.

Kind regards,
Mark

Nellore Raveend
Tera Contributor

Hi Mark ,

 

In second screen shot(delegation issue 2) i have written the script for that by using error message only we are not going to submit that form .

 

Thanks, Raveendra

Nellore Raveend
Tera Contributor

Hi Mark , 

 

This Ui policy is working on the table form , earlier i have used g_form.showErrorBox('')  , With this if i will be taking ends field as more than 30 days then automatically we are clearing that ends field . So that we are unable to submit that form .

 

 

Nellore Raveend
Tera Contributor

Hi @Mark Roethof ,

 

Can you please help me with that scipt which i need to include in Ui policy for aborting form submission in service portal ?

 

Nellore Raveend
Tera Contributor

Hi @Mark Roethof ,

Awaiting for your response ,

Could you please help me with that script which i need to include in Ui policy for aborting form submission in service portal ?

Mark Roethof
Tera Patron
Tera Patron

return false;

Kind regards,
Mark

Nellore Raveend
Tera Contributor

Hi @Mark Roethof 

i have written return false in that ui policy script , it is working on the table form view but its not working on Service Portal form view .

For your reference i have attached some screen shots here , Please go through it and help here to solve this .

Ui Policy script :

find_real_file.png

Delegation table form view (working ) :

find_real_file.png

Service Portal Form view (Not working) :

find_real_file.png

Nellore Raveend
Tera Contributor

Updated Ui Policy screen shot :

find_real_file.png

Mark Roethof
Tera Patron
Tera Patron

No, that should be in your onSubmit client script.

Kind regards,
Mark

Nellore Raveend
Tera Contributor

I created onsubmit client script and mentioned return false in that . It is working on service portal form view as well but after clicking on Save button it remains on the same page  no alert message showing in service portal . we need alert message and later we need to clear ends field value .

below is the script :

find_real_file.png

find_real_file.png

 

 

 

Edxavier Robert
Kilo Sage

Hi @Mark Roethof

I have a question, I have created a UI policy for date and time validation as follows: 

find_real_file.png

 

But if I stay more than a minute on the form it's saying that is in the future. I have auto-populate the date and time just to show 

find_real_file.png

After 1 min on the form, I used the date and time picker and it shows like this

find_real_file.png

Execute if true

function onCondition() {
    g_form.addErrorMessage(getMessage('Entered Date/Time cannot be in the future'));
    g_form.clearValue('u_when_this_ocurr');
}

I know the current time is when the form loads, but how I can create the UI policy to validate the actual date and time of the system and not when the form loads?  

Rajat_Singhal
Tera Expert

Strange behavior, it is not allowing past date that is good.. but it is also not allowing next 9.5hrs. I think this is because of timeZone.. Any suggestion?

Version history
Last update:
‎08-06-2019 08:20 AM
Updated by: