past date validation using BR

sushma9
Tera Contributor

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 :

 var arrival = new GlideDate((current.u_arrival_date);
        var dep = new GlideDate((current.u_departure_date);
        var arrivalFinal = arrival.getDate();
        var depFinal = dep.getDate();
        if (depFinal.compareTo(arrivalFinal) = -1) {
            gs.addErrorMessage(" You have Selected the Past Date . Kindly  select the Future Date ");
            current.setAbortAction(true);
        } else If (depFinal.compareTo(arrivalFinal )= 0)) {
            gs.addInfoMessage(" You have Selected the todays Date . Kindly  Please confirm to submit the request ");
return confirm ();
}else  (depFinal.compareTo(arrivalFinal )= 1)) {
gs.addInfoMessage(" Record has been created");
}
 
i tried with below Code Also:
 
    var arrival = new GlideDate((current.u_arrival_date));
        var dep = new GlideDate((current.u_departure_date));
            var arrivalFinal = arrival.getDate();
            var depFinal = dep.getDate();
            if (depFinal.compareTo(arrivalFinal) = -1) {
                gs.addErrorMessage(" You have Selected the Past Date . Kindly  select the Future Date ");
                current.setAbortAction(true);
            } else if(depFinal.compareTo(arrivalFinal) = 0){
            gs.addInfoMessage(" You have Selected the todays Date . Kindly  Please confirm to submit the request ");
            return confirm();
        } else{
        gs.addInfoMessage(" Record has been created");
    }
2 ACCEPTED SOLUTIONS

Community Alums
Not applicable

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

View solution in original post

Community Alums
Not applicable

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

View solution in original post

5 REPLIES 5

Community Alums
Not applicable

Hello @sushma9 ,

 

Please try below code:

var arrival = new GlideDate(current.u_arrival_date);
var dep = new GlideDate(current.u_departure_date);
var arrivalFinal = arrival.getLocalDate();
var depFinal = dep.getLocalDate();
if (depFinal.compareTo(arrivalFinal) < 0) {
  gs.addErrorMessage("You have selected a past date. Please select a future date.");
  current.setAbortAction(true);
} else if (depFinal.compareTo(arrivalFinal) === 0) {
  gs.addInfoMessage("You have selected today's date. Please confirm to submit the request.");
  return confirm();
} else if (depFinal.compareTo(arrivalFinal) > 0) {
  gs.addInfoMessage("Record has been created.");
}

 

If this helped you in any way, please hit the like button/mark it helpful. So it will help others to get the correct solution.

 

regards,

Prasad

Hi @Community Alums 

Its Not working and getting the below error msg.

sushma9_0-1688548173415.png

 

 

sushma9_1-1688548325672.png

 

Community Alums
Not applicable

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

Hi @Community Alums :

With the above code  i am able to validate the dates but the records are getting created .How to stop the records if the invalid selection is taken.