- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 10:50 AM
Hi all,
I have Date type field called "Reservation Time" and the requirement is, it should not take past date. I have created a onChange Client Script on Reservation Time field.
Requirement :- Date field should not take past date but should take Todays date only if timing is from future.
onChange Client Script
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var reservationDate = new Date(newValue);
var currentDate = new Date();
if (reservationDate < currentDate )
{
g_form.addErrorMessage('Please Select Future date & time'));
g_form.clearValue('u_reservation_time_cb');
return false;
}
This is working as it is not taking any past date but Today's date is also not taking.
What Can I do to achieve this use case. How do I match the timing as well.
Thanks!!
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 11:06 AM - edited 04-19-2023 11:21 AM
Hello @Sid_Takali
Use below code:
OnChange Client Script : field name : date_time
Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading){ return; }
if(newValue != ''){
var ga = new GlideAjax('CheckDate');
ga.addParam('sysparm_name', 'chkCatDate');
ga.addParam('sysparm_date',g_form.getValue('date_time'));
ga.getXML(DatParse);
}
function DatParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date cannot exist in past.");
g_form.setValue('date_time', ''); // Empty the variable.
}}}
___________________________________________________________________________
Now write a script include as follows :-
Name : CheckDate
Client callable : true
Script :
var CheckDate = Class.create();
CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatDate : function() {
var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });
________________________________________________________
OR you could use UI Policy
Table - Table Name
Conditions - Planned Start Date before Today
Execute if true Scripts -
function onCondition() {
alert('Please select future Date & Time');
g_form.clearValue('u-reservation_time_cb');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Priyanka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 11:06 AM - edited 04-19-2023 11:21 AM
Hello @Sid_Takali
Use below code:
OnChange Client Script : field name : date_time
Script :
function onChange(control, oldValue, newValue, isLoading)
{
if(isLoading){ return; }
if(newValue != ''){
var ga = new GlideAjax('CheckDate');
ga.addParam('sysparm_name', 'chkCatDate');
ga.addParam('sysparm_date',g_form.getValue('date_time'));
ga.getXML(DatParse);
}
function DatParse(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
if(answer == 'false'){
alert("Date cannot exist in past.");
g_form.setValue('date_time', ''); // Empty the variable.
}}}
___________________________________________________________________________
Now write a script include as follows :-
Name : CheckDate
Client callable : true
Script :
var CheckDate = Class.create();
CheckDate.prototype = Object.extendsObject(AbstractAjaxProcessor, {
chkCatDate : function() {
var start = this.getParameter('sysparm_date');
var currDay = gs.now();
if(start < currDay){
return false;
}
else
{ return true; } } });
________________________________________________________
OR you could use UI Policy
Table - Table Name
Conditions - Planned Start Date before Today
Execute if true Scripts -
function onCondition() {
alert('Please select future Date & Time');
g_form.clearValue('u-reservation_time_cb');
}
If my response helped please mark it correct and close the thread so that it benefits future readers.
Regards
Priyanka
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 06:46 AM
Hii @Priyanka0402 I got issue here
- when Select a date in the past (a warning message appears)
- when Select a future date nothing will happen as expected
- when Select a past date again, clearing date field but (no warning message appears).
so, what do you think? why its not working?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2023 10:16 PM
Hello @Sid_Takali
I tried it in my Instance, it is working fine. I would suggest you to please check the code once.
Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 11:09 AM
Hi @Sid_Takali,
Restricting dates can also be done almost no code (only code would be the error message) Using UI Policy.
If my answer solved your issue, please mark my answer as ✅ Correct & 👍Helpful based on the Impact.
Regards,
Ranjit