- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 01:46 AM - edited 07-05-2023 01:54 AM
Hi All,
I am trying to restrict the past Date using the BR but i am not able to do it and getting the error .Can any one help me on this . Let me know if any modifications has to be done in the Code
Note : I can restrict by using Client scripts but I want to restrict only using BR and i Am learning the code .
Script :
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 07:39 AM
Hello @sushma9 ,
The following code will ensure that the selected dates(departure and arrival) are not in the past, the arrival date is before or equal to the departure date, and the arrival and departure dates are not the same.
Try the below code:
var arrival = new GlideDateTime(current.u_arrival_date);
var departure = new GlideDateTime(current.u_departure_date);
// Get the current date and time
var now = new GlideDateTime();
// Clear the time portion of the current date
now.setDisplayValue(now.getDate());
// Validate Arrival date and Departure date is not in the past
if (arrival.before(now) || departure.before(now)) {
gs.addErrorMessage("Arrival date and Departure date cannot be in the past. Please select a future date.");
current.setAbortAction(true);
}
// Validate arrival date is less than or equal to departure date
else if (departure.after(arrival)) {
gs.addErrorMessage("Departure date must be before or equal to arival date.");
current.setAbortAction(true);
}
// Validate arrival date is not the same as departure date
else if (arrival.equals(departure)) {
gs.addErrorMessage("You have selected today's date. Please confirm to submit the request.");
return confirm();
}
//Dates are fine
else {
gs.addInfoMessage(" Record has been created");
}
Mark it helpful if this helped you in any way.
-Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:42 PM
Hi @sushma9 ,
By adding the 'return false;' statement after setting current.setAbortAction(true), you prevent the record creation from happening when an invalid date selection is detected. Please refer below code:
var arrival = new GlideDateTime(current.u_arrival_date);
var departure = new GlideDateTime(current.u_departure_date);
// Get the current date and time
var now = new GlideDateTime();
// Clear the time portion of the current date
now.setDisplayValue(now.getDate());
// Validate Arrival date and Departure date is not in the past
if (arrival.before(now) || departure.before(now)) {
gs.addErrorMessage("Arrival date and Departure date cannot be in the past. Please select a future date.");
current.setAbortAction(true);
// Stop the record creation by returning false
return false;
}
// Validate arrival date is less than or equal to departure date
else if (departure.after(arrival)) {
gs.addErrorMessage("Departure date must be before or equal to arrival date.");
current.setAbortAction(true);
// Stop the record creation by returning false
return false;
}
// Validate arrival date is not the same as departure date
else if (arrival.equals(departure)) {
gs.addErrorMessage("You have selected today's date. Please confirm to submit the request.");
return confirm();
}
// Dates are valid, proceed with record creation
else {
gs.addInfoMessage("Record has been created");
// Allow the record creation to proceed by returning true
return true;
}
regards,
Prasad
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-05-2023 10:42 PM
Hi @sushma9 ,
By adding the 'return false;' statement after setting current.setAbortAction(true), you prevent the record creation from happening when an invalid date selection is detected. Please refer below code:
var arrival = new GlideDateTime(current.u_arrival_date);
var departure = new GlideDateTime(current.u_departure_date);
// Get the current date and time
var now = new GlideDateTime();
// Clear the time portion of the current date
now.setDisplayValue(now.getDate());
// Validate Arrival date and Departure date is not in the past
if (arrival.before(now) || departure.before(now)) {
gs.addErrorMessage("Arrival date and Departure date cannot be in the past. Please select a future date.");
current.setAbortAction(true);
// Stop the record creation by returning false
return false;
}
// Validate arrival date is less than or equal to departure date
else if (departure.after(arrival)) {
gs.addErrorMessage("Departure date must be before or equal to arrival date.");
current.setAbortAction(true);
// Stop the record creation by returning false
return false;
}
// Validate arrival date is not the same as departure date
else if (arrival.equals(departure)) {
gs.addErrorMessage("You have selected today's date. Please confirm to submit the request.");
return confirm();
}
// Dates are valid, proceed with record creation
else {
gs.addInfoMessage("Record has been created");
// Allow the record creation to proceed by returning true
return true;
}
regards,
Prasad