HR case should not closed if any HR task is open

Jyoti Mehta
Tera Contributor

Hi,

we have an requirement where a HR case should not be closed until all the HR tasks will be completed. For which we have created a onbefore business rule whenever case state changes to Resolved state then it should be abort and show error message.

we are facing issue for the error message : Here case is abort when click on resolved button but it is not showing the error message. Can anyone please check below script and suggest ?

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

var gr = new GlideRecord('sn_hr_core_task');
gr.addActiveQuery();
gr.addQuery('parent',current.sys_id);
gr.query();
if (gr.next()) {
// gs.addInfoMessage("please close HR tasks first");
gs.addErrorMessage(gs.getMessage('please close HR tasks first'));
current.setAbortAction(true);
}

})(current, previous);

13 REPLIES 13

Susan Britt
Mega Sage
Mega Sage

You can accomplish this with a Client Script (onChange of State field) and Script Include.

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

	if(newValue==3) { //Closed Complete
		var ga = new GlideAjax('HRScriptInclude');
		ga.addParam('sysparm_name', 'validateChildTasks');
		ga.addParam('sys_id', g_form.getUniqueValue());
		ga.getXML(setValues);
	}
        
	function setValues(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');
		if(answer == "true") {
			g_form.addErrorMessage(gs.getMessage("hr_open_tasks_exist"));
			g_form.setValue('state',oldValue);
		} else {
			g_form.setValue('state',newValue);
		}
	}
}

 

Script Include Function

validateChildTasks: function(){
		var sys_id = this.getParameter('sys_id');
		var flag = false;
		var taskRec = new GlideRecord('task');
		taskRec.addQuery('parent', sys_id);
		taskRec.addActiveQuery();
		taskRec.query();
		while (taskRec.next()) {
			flag = true;
		}
		return flag;
	},

Hi Sbritt,

we have tried this option and it is showing error message correctly but case state is changing to Resolved state. Please suggest. 

Thanks,

Jyoti 

The example code is only checking for changing to Closed Complete, so you can update it to include any other values like Resolved also.   

if(newValue==3) { //Closed Complete

I have updated the newvalue == 20 // resolved state but still it is changing the state.