Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Adding days excluding weekends

Paulo Campos Ch
Tera Contributor

I have a requirement where I need the task called CMDB, once created, in the workflow to assign the planned end date with two more days to the planned start date, but not count weekends.

I have a business rule programmed where it takes the value of the due date field of the task creation and places it in the planned completion date field, but I cannot get the two additional days to be reflected without counting weekends

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron

Hi @Paulo Campos Ch,

 

You posted BR has an Action that sets the planned_start_date to the due_date, and also has "Advanced" check, but you didn't post your script. You should use one means and not both.  The following BR script can do what you want:

 

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

	// Add your code here
	var dateValue = current.planned_start_date;
	gs.info("CalcEndDate: planned_start_date = " + dateValue);
	var daysToAdd = 2;
	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('090eecae0a0a0b260077e1dfa71da828');
	var startDate = new GlideDateTime(dateValue);
	gs.info('CalcEndDate: checking if start date: ' + startDate + ' is in schedule');
	if (!sched.isInSchedule(startDate)) {
		gs.info('CalcEndDate: 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('CalcEndDate: checking if ' + newDate + ' is in schedule');
		while (!sched.isInSchedule(newDate)) {
			gs.info('CalcEndDate: ' + newDate + ' is not in schedule');
			newDate.addDays(1);
		}
	}
	current.planned_end_date = newDate;
	gs.info("CalcEndDate: due_date: " + dateValue + ", daysToadd: " + daysToAdd + ", newDate = " + newDate);

})(current, previous);

Remove the 'Set field value' "Action" and let the script determine the new value.  Provided you want to avoid non-work hours and holidays too.  If that is not a goal, then use the logic in the "if" block above that just checks day-of-week. (And remove any gs.info() lines used to debug if you like.)

View solution in original post

2 REPLIES 2

Bert_c1
Kilo Patron

Try using the "8-5 weekdays" schedule, the table is 'cmn_schedule'. And refer to:

 

addDays API

 

Post your BR definition and script, then folks here can assist.

 

Bert_c1
Kilo Patron

Hi @Paulo Campos Ch,

 

You posted BR has an Action that sets the planned_start_date to the due_date, and also has "Advanced" check, but you didn't post your script. You should use one means and not both.  The following BR script can do what you want:

 

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

	// Add your code here
	var dateValue = current.planned_start_date;
	gs.info("CalcEndDate: planned_start_date = " + dateValue);
	var daysToAdd = 2;
	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('090eecae0a0a0b260077e1dfa71da828');
	var startDate = new GlideDateTime(dateValue);
	gs.info('CalcEndDate: checking if start date: ' + startDate + ' is in schedule');
	if (!sched.isInSchedule(startDate)) {
		gs.info('CalcEndDate: 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('CalcEndDate: checking if ' + newDate + ' is in schedule');
		while (!sched.isInSchedule(newDate)) {
			gs.info('CalcEndDate: ' + newDate + ' is not in schedule');
			newDate.addDays(1);
		}
	}
	current.planned_end_date = newDate;
	gs.info("CalcEndDate: due_date: " + dateValue + ", daysToadd: " + daysToAdd + ", newDate = " + newDate);

})(current, previous);

Remove the 'Set field value' "Action" and let the script determine the new value.  Provided you want to avoid non-work hours and holidays too.  If that is not a goal, then use the logic in the "if" block above that just checks day-of-week. (And remove any gs.info() lines used to debug if you like.)