Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

Business rules for change start and end date and change tasks

Rick Larson
Tera Contributor

Hello everyone, I would like to create a BR so that when the user changes the date of start/end the change, I can check if the dates of the change tasks are within the dates (start/end) that were filled in, so that if they were not, send an alert to also change the dates of the tasks, I don't know exactly how to do it.. could someone help me?

I created this script but it sends the alert regardless of whether the dates are correct or not.

 

 

 

(function executeRule(current, previous /*null when async*/ ) {
var areDatesWithinRange = function(startRangeDate, endRangeDate, dates) {
for (var i = 0; i < dates.length; i++) {
if (!dates[i])
continue;
if (gs.dateDiff(startRangeDate, dates[i], true) < 0 || gs.dateDiff(endRangeDate, dates[i], true) > 0)
return false;
}
return true;
};
var gr = new GlideRecord('change_task');
gr.addEncodedQuery('change_request='+ current.sys_id);
gr.query();
if(gr.next()){
var StartDate = gr.planned.start_date.getDisplayValue();
var EndDate = gr.planned.end_date.getDisplayValue();
if (!areDatesWithinRange(StartDate,EndDate ,[current.start_date,current.end_date,])) {
gs.addErrorMessage(gs.getMessage("Test"));
current.setAbortAction(true);
}
}
})(current, previous);

 

2 REPLIES 2

OlaN
Tera Sage

Hi,

So if I understand this correctly, you want to evaluate if the start and end date of attached change tasks are within the same range as the parent change record.

Then I would write a business rule that runs on change_request table, with condition as start date or change date changes, and in the script write something like this:

 

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

	var changeTaskGR = new GlideRecord('change_task');
	changeTaskGR.addActiveQuery();
	changeTaskGR.addQuery('change_request', current.getUniqueValue());
	changeTaskGR.query();
	
	if (!changeTaskGR.hasNext()){ // no tasks found
		return;
	}
	else{
		var changeStart = new GlideDateTime(current.getValue('start_date'));
		var changeEnd = new GlideDateTime(current.getValue('end_date'));
		while(changeTaskGR.next()){
			var taskStart = new GlideDateTime(changeTaskGR.getValue('planned_start_date'));
			if (taskStart.before(changeStart)){
				gs.addInfoMessage('Change task is set before change start: ' + changeTaskGR.getValue('number'));
			}
			var taskEnd = new GlideDateTime(changeTaskGR.getValue('planned_end_date'));
			if (taskEnd.after(changeEnd)){
				gs.addInfoMessage('Change task is set after change end: ' + changeTaskGR.getValue('number'));
			}
		}
	}

})(current, previous);

 

Kasi Dhanabalan
Tera Expert

Please find the below script to achieve the requirement.

Business Rule

Table: Change_Request

When to Run:

KasiRamanatha1_0-1665733594596.png

Script:

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

    // Add your code here
    var plannedStartDateChange = new GlideDateTime(current.start_date);
    var plannedEndDateChange = new GlideDateTime(current.end_date);
    gs.log('Planned Start Date is ' + plannedStartDateChange + ' and the Planned End Date is ' + plannedEndDateChange + ' on the change ' + current.number);

    var changeTask = new GlideRecord('change_task');
    changeTask.addQuery('change_request', current.sys_id);
    changeTask.query();
    while (changeTask.next()) {
        var plannedStartDateCTask = new GlideDateTime(changeTask.planned_start_date);
        var plannedEndDateCTask = new GlideDateTime(changeTask.planned_end_date);
        gs.log('Planned Start Date is ' + plannedStartDateCTask + ' and the Planned End Date is ' + plannedEndDateCTask + ' on the change task ' + changeTask.number);
        if (plannedStartDateCTask == '' || plannedEndDateCTask == '') {
            gs.addErrorMessage('The CTask ' + changeTask.number + ' is having empty value in Planned Start Date / Planned End Date');
        } else if (plannedStartDateCTask >= plannedStartDateChange && plannedEndDateCTask <= plannedEndDateChange) {
            gs.addErrorMessage('It\'s within Range');
        } else {
            gs.addInfoMessage('Error! Not within Range');
        }
    }


})(current, previous);