How to restrict past and future Date using Business rules

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 

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");
}
1 ACCEPTED SOLUTION

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @sushma9 

 

First thing ((current.u_arrival_date), there is extra braces '(' you used.

second thing compareTo is generally used in string type and date not in string it is object

 

you can use code like below, if comparision on today

 

var dateField = new GlideDateTime(current.u_arrival_date);;

var nowTime = new GlideDateTime(); // current date and time

var dur = new GlideDuration();

dur = GlideDateTime.subtract(dateField, nowTime);

var days = dur.getDayPart();

gs.info(days);

if(days>0){
gs.info("Arrival date can not be in past");

}

 

Please check and Mark Helpful or correct if it really helps you.

View solution in original post

11 REPLIES 11

Priyanka0402
Mega Sage
Mega Sage

Hello @sushma9 , there are some issues like extra parentheses and the comparison operator.

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards

Priyanka

 

 

HI @Priyanka0402 

Its not working

Check these loops. 

 

 

Kalyani Jangam1
Mega Sage
Mega Sage

Hi @sushma9 

 

First thing ((current.u_arrival_date), there is extra braces '(' you used.

second thing compareTo is generally used in string type and date not in string it is object

 

you can use code like below, if comparision on today

 

var dateField = new GlideDateTime(current.u_arrival_date);;

var nowTime = new GlideDateTime(); // current date and time

var dur = new GlideDuration();

dur = GlideDateTime.subtract(dateField, nowTime);

var days = dur.getDayPart();

gs.info(days);

if(days>0){
gs.info("Arrival date can not be in past");

}

 

Please check and Mark Helpful or correct if it really helps you.