- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 02:46 AM
I have written above onchange catalog client script to not allow date field accept past dates. it is not working. kindly suggest.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 02:54 AM
Here you go, , just change 'delivery_date' to your variable name.
Recommendation: try using UI Policies to perform date validation, it's much easier/quicker and can be done without scripting. Take a look at this article: https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideAllFieldMsgs();
var newDateNumb = dateToNumber(newValue); // converts newValue to an integer as yyyymmdd
var todDate = todaysDate();
var todDateNum = dateToNumber(todDate); // converts todays date to an integer as yyyymmdd
if (newDateNumb < todDateNum) {
g_form.setValue('delivery_date', '');
g_form.showFieldMsg('delivery_date', 'This date cannot be in the past, please enter another date', 'error');
return;
}
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 03:14 AM
Hi @NiloferS,
It looks like you are trying to implement a catalog UI policy in ServiceNow to ensure that a date field on a catalog item does not allow past dates. Here is how you can achieve this:
1. Create a Catalog UI Policy:
- Go to `Service Catalog > Catalog Policies > Catalog UI Policies` in the Application Navigator.
- Click on the `New` button to create a new Catalog UI Policy.
2. Set Conditions:
- In the `When to Apply` tab, set the `Catalog Conditions` as follows:
- Field: `Date` (or the specific date field you are working with)
- Operator: `Relative`
- Comparison: `Before`
- Value: `1 minute ago` (This ensures the date is not in the past)
3. Add UI Policy Actions:
- Go to the `Script` tab to add your custom script.
- Here, you can write a script to set the date field as mandatory or read-only based on the condition.
Here's an example script for the `Script` tab:
function onCondition() {
// Ensure that the date is not in the past
g_form.setReadOnly('your_date_field', true);
g_form.setMandatory('your_date_field', true);
g_form.showFieldMsg('your_date_field', 'The selected date cannot be in the past', 'error');
}
4. Save and Test:
- Save the UI policy and test it by going to the relevant catalog item form to ensure the date field is validated as expected.
Thank you, please make helpful if you accept the solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 02:51 AM
Hi,
You can use a simple no code UI policy for the same, like below
In the script part you can clear the variable and throw error.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 02:54 AM
Here you go, , just change 'delivery_date' to your variable name.
Recommendation: try using UI Policies to perform date validation, it's much easier/quicker and can be done without scripting. Take a look at this article: https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
g_form.hideAllFieldMsgs();
var newDateNumb = dateToNumber(newValue); // converts newValue to an integer as yyyymmdd
var todDate = todaysDate();
var todDateNum = dateToNumber(todDate); // converts todays date to an integer as yyyymmdd
if (newDateNumb < todDateNum) {
g_form.setValue('delivery_date', '');
g_form.showFieldMsg('delivery_date', 'This date cannot be in the past, please enter another date', 'error');
return;
}
Regards
Paul
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 02:54 AM - edited ‎06-19-2024 02:57 AM
Hi @NiloferS ,
You can user Catalog UI policy instead of Client Script, Please check below thread.
No Code date validations through (Catalog) UI Policies
Thanks,
Trupti
Please consider marking this as helpful/correct if it has been beneficial to you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-19-2024 03:14 AM
Hi @NiloferS,
It looks like you are trying to implement a catalog UI policy in ServiceNow to ensure that a date field on a catalog item does not allow past dates. Here is how you can achieve this:
1. Create a Catalog UI Policy:
- Go to `Service Catalog > Catalog Policies > Catalog UI Policies` in the Application Navigator.
- Click on the `New` button to create a new Catalog UI Policy.
2. Set Conditions:
- In the `When to Apply` tab, set the `Catalog Conditions` as follows:
- Field: `Date` (or the specific date field you are working with)
- Operator: `Relative`
- Comparison: `Before`
- Value: `1 minute ago` (This ensures the date is not in the past)
3. Add UI Policy Actions:
- Go to the `Script` tab to add your custom script.
- Here, you can write a script to set the date field as mandatory or read-only based on the condition.
Here's an example script for the `Script` tab:
function onCondition() {
// Ensure that the date is not in the past
g_form.setReadOnly('your_date_field', true);
g_form.setMandatory('your_date_field', true);
g_form.showFieldMsg('your_date_field', 'The selected date cannot be in the past', 'error');
}
4. Save and Test:
- Save the UI policy and test it by going to the relevant catalog item form to ensure the date field is validated as expected.
Thank you, please make helpful if you accept the solution.