Business rule to update the active to false

RudhraKAM
Tera Guru

My requirement is I have a UI action on Incident to create a problem, which is working fine , but when the incident is closed then the related Problem is setting to active false ,, this is happening from the below BR , not sure where to change.( requirement is if incident is closed it should not close the related problem) as this is global BR not sure where to change and what to change .

find_real_file.png

 

//
// Close any tasks that are related to the current task
//
function closeRelatedTasks(me) {
	var task = new GlideRecord("task");
	task.addQuery("parent", me.sys_id);
	task.addQuery("sys_class_name", "!=", "kb_submission");
	task.addQuery("sys_class_name", "!=", "incident");
	task.query();
	while (task.next()) {
		if (task.sys_class_name == 'problem')
			closeProblem(task.sys_id, me.number);
		else if (task.sys_class_name == 'change_request')
			closeChange(task.sys_id, me.number);
		else {
			task.active.setValue(false);
			task.update();
			gs.print("Task " + task.number + ' closed based on closure of task ' + me.number);
		}
	}
	
	this.closeIncident(me);
}

function closeIncident(me) {
	var incident = new GlideRecord('incident');
	if (incident.isValidField('parent_incident'))
		incident.addQuery('parent_incident', me.sys_id);
	else if(incident.isValidField('parent'))
			incident.addQuery('parent', me.sys_id);
		else
			return;
	incident.addActiveQuery();
	incident.query();
	while (incident.next()) {
		var msg = gs.getMessage("Incident '{0}' closed based on closure of task '{1}'", [incident.number, me.number]);
		gs.print(msg);
		incident.incident_state.setValue(IncidentState.CLOSED);
		incident.active.setValue(false);
		incident.comments = msg;
		var notes = incident.close_notes.getDisplayValue();
		notes = notes + '\n' + msg + '\n\n' + me.close_notes;
		incident.close_notes.setValue(notes);
		incident.close_code.setValue(me.close_code);
		incident.update();
	}
}

function closeProblem(myID, fromNumber) {
	if (pm.isActive('com.snc.best_practice.problem.madrid.state_model'))
		return;
	var problem = new GlideRecord('problem');
	if (problem.get('sys_id', myID)) {
		var msg = gs.getMessage("Problem '{0}' closed based on closure of task '{1}'", [problem.number, fromNumber]);
		problem.problem_state.setValue(4);
		var notes = problem.close_notes.getDisplayValue();
		notes = notes + '\n' + msg;
		problem.close_notes.setValue(notes);
		problem.update();
		gs.print(msg);
	}
}

function closeChange(myID, fromNumber) {
	var rfc = new GlideRecord('change_request');
	if (rfc.get('sys_id', myID)) {
		var chgReq = ChangeRequest(rfc);
		
		if (!chgReq.isClosed() && !chgReq.isCancelled()) {
			chgReq.close();
			var msg = gs.getMessage("Change '{0}' closed based on closure of task '{1}'", [rfc.number, fromNumber]);
			var notes = rfc.close_notes.getDisplayValue();
			notes = notes + '\n' + msg;
			rfc.close_notes = notes;
			rfc.update();
		}
	}
}

 

1 ACCEPTED SOLUTION

DirkRedeker
Mega Sage

Hi

If you cannot edit the Business Rule, make sure, that you are working with an user having admin privileges.

Furthermore, as this Business Rule is set to the Global Scope, you need to work in global scope. Otherwise, the Code and fields may be read-only.

Another option is, to copy the whole Business rule, and leave the OOB as is, but only DE-Activate that.

In the copy, do the changes you want, and set that to active.

 

That way, you do not loose the OOB Rule content, and can modify your copy as desired.

 

Let me know, if that answered your question, and mark my answer as correct and helpful. please.

 

BR

Dirk

View solution in original post

3 REPLIES 3

Community Alums
Not applicable

If you don't mind problems not being closed under any circumstance, you can comment out the parts in yellow.

 

find_real_file.png

 

That would be the fastest method of fixing your issue, even if its not the most desirable.

DirkRedeker
Mega Sage

Hi

If you cannot edit the Business Rule, make sure, that you are working with an user having admin privileges.

Furthermore, as this Business Rule is set to the Global Scope, you need to work in global scope. Otherwise, the Code and fields may be read-only.

Another option is, to copy the whole Business rule, and leave the OOB as is, but only DE-Activate that.

In the copy, do the changes you want, and set that to active.

 

That way, you do not loose the OOB Rule content, and can modify your copy as desired.

 

Let me know, if that answered your question, and mark my answer as correct and helpful. please.

 

BR

Dirk

Abhinay Erra
Giga Sage

Do not use global business rules. Instead copy the code in to a script include and call the function. Your closeRelatedTasks function will look like this

function closeRelatedTasks(me) {
	var task = new GlideRecord("task");
	task.addQuery("parent", me.sys_id);
	task.addQuery("sys_class_name", "!=", "kb_submission");
	task.addQuery("sys_class_name", "!=", "incident");
        task.addQuery("sys_class_name","!=","problem");
	task.query();
	while (task.next()) {
		if (task.sys_class_name == 'change_request')
			closeChange(task.sys_id, me.number);
		else {
			task.active.setValue(false);
			task.update();
			gs.print("Task " + task.number + ' closed based on closure of task ' + me.number);
		}
	}
	
	this.closeIncident(me);
}