The Now Platform® Washington DC release is live. Watch now!
on ‎08-06-2019 08:20 AM
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.
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.
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)
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
---
Nice article, it's always good to see other people ideas on resolving common issues. Your solution is really helpful.
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:
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.
Here is the Portal, no matter the date I select
In the platform I dont have that problem when trying it from the Service Catalog. Any suggestion?
Hi Mark,
Thank you for the Article, Bookmarked is sure thing.
I have one query that it is possible to exclude holidays too.
Thanks,
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
---
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,
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;
}},
Thanks for the article, Mark!!.
This saved me a hell lot of time... 🙂
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
Hi Steve, Were you able to resolve this issue? I have the same problem with portal
Hi Samadam,
Please uncheck the onLoad check box and try.
Pls share your error screen shot and UI policy.
Thanks,
Harish M
Hi Mark,
I'm trying to do hours validation in Duration Field on Service Portal. Any suggestions?
Thanks,
Ashraf
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
Hello,
Can I restrict a Planned Start Date for Change Requests using this????
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
---
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.
Its related, date validation, except for a Change Request
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
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?
Hi
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
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
---
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?
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
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.
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
---
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.
I was asking myself why didn't I see this article earlier. It was so helpful and easy.
thanks
Sajith
Hi Mark,
Me too facing issue, not getting expected output. In Quebec version.
Could you please help me with UI policy which can throw error if we select past date and date beyond 180 days...
Can anyone please help me with UI policy which can throw error if we select past date and date beyond 180 days...
Can you share what you tried and what's not working?
Kind regards,
Mark
Hi Mark,
Your solution is very helpful, It's working fine.
Thank's
How can I disable the past dates so that user cannot pick them?
Hi Mark, Will it work for multi-user environment where users are spread across globe? They work in different time zones.
Thanks.
Hi
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
Hi there,
Can you share details?
Kind regards,
Mark
Hello
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 :
Delegate Form: Selected ends date as more than 30 days
After Submitting the form getting error message and within few secs redirects to blank page .
redirected to blank page :
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 .
So where are you aborting to submission? Don't see that you added anything for this for the portal.
Kind regards,
Mark
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
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 .
Hi
Can you please help me with that scipt which i need to include in Ui policy for aborting form submission in service portal ?
Hi
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 ?
return false;
Kind regards,
Mark
Hi
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 :
Delegation table form view (working ) :
Service Portal Form view (Not working) :
Updated Ui Policy screen shot :
No, that should be in your onSubmit client script.
Kind regards,
Mark
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 :
Hi
I have a question, I have created a UI policy for date and time validation as follows:
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
After 1 min on the form, I used the date and time picker and it shows like this
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?
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?