'Planned Start date' should be after 3 days of creation

prakhar_yadav
Tera Contributor

Hi Community,

 

In a change_request form, if model is 'Normal' and creation of change request is today, then in this case 'Planned Start Date' should be after 3 business days (weekends must be excluded). If user tries to select time less than 3 days, error should be visible to set any date after 3 days. 

Kindly help in achieving this, if you will provide proper script that will be highly appreciated.

 

Thank You

1 ACCEPTED SOLUTION

Hayo Lubbers
Kilo Sage

Hi @prakhar_yadav ,

 

You can use the condition builder from the UI Policy for this.

HayoLubbers_0-1716368065799.png

And then you can show a message and clear the value via the script:

HayoLubbers_1-1716368568025.png

 

 

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron

After re-reading your post, seems a business rule can work, using the logic in the script include. If the calculated date is less that 'current.start_start date', the set it to the calculated date, add message and abort the submission.

 

 

 

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

		var dateValue = current.sys_created_on;
//      gs.info("checkPlannedEndDate: called, date = " + dateValue);
		var daysToAdd = 3;
		var newDate = new GlideDateTime(dateValue);
		// Add days
        newDate.addDays(daysToAdd);

		// Check if start date is in the schedule (8-5 weekdays excluding holidays)
		var sched = new GlideSchedule();
		sched.load('08fcd0830a0a0b2600079f56b1adb9ae');  // 8-5 weekdays
		var startDate = new GlideDateTime(dateValue);
//		gs.info('checkPlannedEndDate: checking if start date: ' + startDate + ' is in schedule');
		if (!sched.isInSchedule(startDate)) {
//			gs.info('checkPlannedEndDate: Start date is not in schedule! Using day-of-week.');
			var dayOfWeek = newDate.getDayOfWeekLocalTime();
			switch (dayOfWeek) {
				case 6:
					newDate.addDays(2);		//Saturday
					break;
				case 7:
					newDate.addDays(1);		// Sunday
					break;
			}
		}
		else {
			// Check if new date is in the schedule (8-5 weekdays excluding holidays)
//			gs.info('addDaysTodate: checking if ' + newDate + ' is in schedule');
			while (!sched.isInSchedule(newDate)) {
//				gs.info('addDaysTodate: ' + newDate + ' is not in schedule');
				newDate.addDays(1);
			}
		}
		if (dateValue < newDate.getValue()) {
			gs.addErrorMessage("Planned start must be three days from now.");
			current_start_date = newDate;
			current.setAbortAction(true);
		}

})(current, previous);

 

 

 

 

 

Define that on the change_request table, When to run follows:

Screenshot 2024-05-22 124422.png

Simplier script, no schedule needed:

 

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

	var dateValue = current.sys_created_on;
//	gs.info("checkPlannedEndDate: called, date = " + dateValue);
	var daysToAdd = 3;
	var newDate = new GlideDateTime(dateValue);
	// Add days
	newDate.addDays(daysToAdd);
	// See if new date is on a weekend
	var dayOfWeek = newDate.getDayOfWeekLocalTime();
	switch (dayOfWeek) {
		case 6:
			newDate.addDays(2);		//Saturday
			break;
		case 7:
			newDate.addDays(1);		// Sunday
			break;
	}
	if (dateValue < newDate.getValue()) {
		gs.addErrorMessage("Planned start must be three days from now.");
		current_start_date = newDate;
		current.setAbortAction(true);
	}

})(current, previous);