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

Hi @Kalyani Jangam1 

its not working

Hi @sushma9 

what's issue you are facing??

It is working on my end.

Hi @Kalyani Jangam1 

Its working now and i have added some other code and executed its working fine but return confirm(); is not working for me.

Script :

 var dateField = new GlideDateTime(current.u_arrival_date);
     var dep = new GlideDateTime((current.u_departure_date));
    var dur = new GlideDuration();
    dur = GlideDateTime.subtract(dep, dateField);
    var days = dur.getDayPart();
    if (days > 0) {
        current.setAbortAction(true);
        gs.addErrorMessage("Departure Date Cannot  be in the Past Date ");
    }else if(days == 0){
    gs.addInfoMessage("Departure Date selected as Today .Kindly please confirm to create the record "); 
    return confirm() ;

    }else{

        gs.addInfoMessage("Record Has been succesfully  Created ");
    }

Hi @Kalyani Jangam1 

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.

Ankur Bawiskar
Tera Patron
Tera Patron

@sushma9 

why to use business rule for this?

why not handle this using onchange or onSubmit script

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader