business rule to restrict date field selection

vinnus
Tera Contributor

business rule to restrict start date field to select from tomorrow only and end date field is from yesterday. 

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi @vinnus ,

Oh okayy now I'm clear with your reqiurments you can use below code now 

It will work for you

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var currentDate = new GlideDateTime();
    var startDate = current.start_date;
    var endDate = current.end_date;
    var convertStartDate = new GlideDateTime(startDate);
    var convertEndDate = new GlideDateTime(endDate);

    gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
    if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
        gs.log("Inside if 2");
        gs.addErrorMessage("Can not select from future");
        current.setAbortAction(true);
    } 
	
})(current, previous);

Result 

SarthakKashyap_0-1714635260047.png

Highlighted date is selected from future , when I save this record it shows me the error 

SarthakKashyap_1-1714635345241.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

View solution in original post

8 REPLIES 8

Community Alums
Not applicable

Hi @vinnus ,

I tried your problem in my PDI and it works for me please refer below script 

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var currentDate = new GlideDateTime();
    var startDate = current.start_date;
    var endDate = current.end_date;
    var convertStartDate = new GlideDateTime(startDate);
    var convertEndDate = new GlideDateTime(endDate);

gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
    if (currentDate.compareTo(convertEndDate) == 1 || currentDate.compareTo(convertEndDate) == 0) {
		gs.log("Inside if 1")
        gs.addErrorMessage("Can not select from past");
        current.setAbortAction(true)
    }
    if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
		gs.log("Inside if 2")
        gs.addErrorMessage("Can not select from future");
        current.setAbortAction(true)
    }

})(current, previous);

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak

Hi Sarthak,

 

BR to restrict the selection of date field  from current and future dates and can only select past dates from yesterday onwards and clear the field if a current or future date is selected
Thanks
 
 

Hi @vinnus 

Just create a BR with trigger condition and

then use this script

(function executeRule(current, previous /*null when async*/) {

	currentDate=new GlideDateTime().getDate();
	endDate=new GlideDateTime(current.work_end).getDate();
	if(endDate==currentDate||endDate>currentDate)
	{
		gs.addErrorMessage('End date should not be not current or future date');
		current.setAbortAction(true);
	}
})(current, previous);

Also check the time zone and validate accordingly.

 

Thanks & Regards

Seraj

Community Alums
Not applicable

Hi @vinnus ,

Oh okayy now I'm clear with your reqiurments you can use below code now 

It will work for you

(function executeRule(current, previous /*null when async*/ ) {

    // Add your code here
    var currentDate = new GlideDateTime();
    var startDate = current.start_date;
    var endDate = current.end_date;
    var convertStartDate = new GlideDateTime(startDate);
    var convertEndDate = new GlideDateTime(endDate);

    gs.log('Inside Restrict Date ' + currentDate + " SatrtDate = " + convertStartDate + " End Date = " + convertEndDate);
    if (currentDate.compareTo(convertStartDate) == -1 || currentDate.compareTo(convertStartDate) == 0) {
        gs.log("Inside if 2");
        gs.addErrorMessage("Can not select from future");
        current.setAbortAction(true);
    } 
	
})(current, previous);

Result 

SarthakKashyap_0-1714635260047.png

Highlighted date is selected from future , when I save this record it shows me the error 

SarthakKashyap_1-1714635345241.png

 

Please mark my answer correct and helpful if this works for you

 

Thanks and Regards 

Sarthak