- 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:42 AM
Hi @Ranjit Nimbalka can you brief me about this?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-19-2023 11:57 AM - edited 04-19-2023 11:57 AM
Hello @Sid_Takali
With 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:24 AM
Here is an onChange Client Script I am using for Change Requests, but it will be easy to modify to your needs:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var cType = g_form.getValue('u_change_type');
var now = new Date();
var value = new Date(g_form.getValue('start_date'));
if (value < now && cType == 'Standard') {
g_form.flash('start_date', '#E6E8EA', -4);
g_form.showErrorBox('start_date', 'Planned Start cannot be in the past');
return false;
}
else {
g_form.hideErrorBox('start_date');
}
}