'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

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

 

 

Hi @Hayo Lubbers ,

 

Thank you for your response, it works partially as it is also taking weekends in the "Planned Start Date" field. How can we exclude weekends (Saturdays and Sundays) so that, user should get the error when they are selecting Saturday or Sundays on the "Planned Start Date" field.

Hi @prakhar_yadav ,

 

I don't think this is possible in the UI script. Let me think about it, I'll get back on it.

 

Hi @prakhar_yadav ,

 

The schedule as mentioned from @Bert_c1 is a nice addition and works probably good when you want to do only serverside scripting.

If you want to warn the user when filling in the form, you can combine serverside scripting (display business rule) and a client script.

 

The display business rule populates the scratchpad with the calculated minimal start date and the change type, the client script checks the type and compares the dates.

 

BR:

(function executeRule(current, previous) {
	var gdtToday = new GlideDateTime();

	var dayOfWeek = gdtToday.getDayOfWeekUTC();
    var daysToAdd = 3; //Number of days to add to the created on or current date
    if (dayOfWeek != 1 && dayOfWeek != 2) {
		if(dayOfWeek == 0){
			daysToAdd += 1; //Sunday
		} else {
			daysToAdd += 2; //Not Monday or Tuesday
		}
    }

	var startDate = gdtToday; //Start with todays date
	if(!current.isNewRecord()){
		startDate.setValue(current.getValue('sys_created_on')); //If it is not a new record, use the created on
	}
	startDate.addDaysUTC(daysToAdd); //Add the 3, 4 or 5 days to the current date or sys_created_on
	var minStartDate = startDate.getLocalDate(); //Delivers a date object in the user timezone

	g_scratchpad.plannedStartDate = {
		"minStartDate": minStartDate.toString(),
		"changeType": current.getValue('type')
	};
})(current, previous);

 

Client Script onChange of the Planned Start Date:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '') {
		return;
	}

	var plannedStartDateData = g_scratchpad.plannedStartDate;
	var changeType = plannedStartDateData.changeType; //Default loaded for changes
	if (changeType == 'normal') {

		var minStartDate = plannedStartDateData.minStartDate;
		var minStartDateObj = new Date(minStartDate);
		var plannedStartDateObj = new Date(getDateFromFormat(newValue, g_user_date_time_format)); //Delivers a properly formatted date object (fixes the dd-MM-yyy or mm/dd/yyy differences)

		//Check if the start date is after the minimal start date
		if (plannedStartDateObj < minStartDateObj) {
			g_form.clearValue('start_date'); //Clear the field
			g_form.showFieldMsg('start_date', 'The date should be at least 3 workingdays in the future', 'error');
		} else {
			g_form.hideFieldMsg('start_date'); //Remove the field message
		}
	}

}