Past date limitation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2024 01:49 AM
I have a date variable on a catalog item form. User will manually select the date.
I have to set limitation on past date.
Can anyone please help me out.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2024 07:14 AM
Hi @Anubhav24
Thanks for your reply. I had tried with UI policy script. There is an issue:
For example, I am setting the date field value as today's date then it is working fine on today and request is raised but when I am checking it tomorrow that date field value is cleared automatically.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2024 11:38 PM
Hi @abhisek ,
Can you check if any other UI Policy or client script is written on page load of the form you are opening and it is clearing the value on the date field if you open it tomorrow , for this scenario (
For example, I am setting the date field value as today's date then it is working fine on today and request is raised but when I am checking it tomorrow that date field value is cleared automatically.)
Please mark helpful/correct if my response helped you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2024 05:57 AM
One solution to your problem would be.
1. Add another date variable (like created date or requested date) and disable it.
2. Set it to Today's date when empty.
3. Compare the User's date with that date rather than comparing it to Today.
Please mark helpful if this resolves your issue.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2024 06:15 AM
Hi @abhisek ,
Hope you are doing well.
Proposed Solution
In order to achieve this task, you have 2 suitable options to get the solution of the query and I personally tried it on my Personal Developer Instance to get the solution as soon as possible:-
1. Create "onChange-Catalog Client Script" on the Date Field and "Script Include" and make a "GlideAjax" call with this. For reference, check below scripts:-
Script Include:-
var RestrictSelectionofPastDate = Class.create();
RestrictSelectionofPastDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
restrictPastDate: function() {
var date = this.getParameter('sysparm_date');
var curDate = gs.now();
if (date < curDate) {
return false;
} else {
return true;
}
},
type: 'RestrictSelectionofPastDate'
});
Client Script:-
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var ga = new GlideAjax('global.RestrictSelectionofPastDate');
ga.addParam('sysparm_name', 'restrictPastDate');
ga.addParam('sysparm_date', g_form.getValue('select_date'));
ga.getXML(getValidation);
}
function getValidation(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer == 'false') {
alert("You can't select the Past Date. Kindly choose the date of today or after it.");
g_form.setValue('select_date', ''); // Empty the variable.
}
}
2. Create "Catalog UI Policy" and add the Filter Condition and write script like down below.
For your reference, also attaching screenshots of the outputs that will give you better insights of how the scripts are working or the best thing will be to follow the solution and execute the scripts on your instance.
If you find this information/knowledge/solution helpful, Please don't forget to mark my solution and reply as helpful and accepted.
Thanks ‌‌
Aakash Garg
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-29-2024 07:13 AM
Hi @AakashG2703
Thanks for your reply. I had tried with UI policy script. There is an issue:
For example, I am setting the date field value as today's date then it is working fine on today and request is raised but when I am checking it tomorrow that date field value is cleared automatically.