Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business rule for when any task is open you cannot resolve the incident.

The_Flash
Mega Guru

Business rule for when any open/unresolved task is available then you cannot change you incident state to resolved, it should throw error message.

 

 

1 ACCEPTED SOLUTION

The_Flash
Mega Guru

My Solution:

When to Run : Before Update

Condition : State Changes to Resolved

 

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

// Add your code here

 

var taskstate = new GlideRecord('sc_task');


taskstate.addQuery('parent', current.sys_id);


taskstate.addEncodedQuery('state!=3');


taskstate.query();

 

if (taskstate.next()) {
gs.addErrorMessage("cannot resolve the incident because the task/s is not resolved");
current.setAbortAction(true);
gs.setRedirect(current);
}

})(current, previous);

View solution in original post

4 REPLIES 4

Pranav_Thanedar
Mega Sage

Hi @The_Flash ,

 

You can create a Before Update business rule and use the below scipt

 

    if (current.state == '6' || current.state == '7' || current.state == '8') {
        var getIncidentTaskRecord = new GlideRecord('incident_task');
        getIncidentTaskRecord.addQuery('incident', current.sys_id);
        getIncidentTaskRecord.addQuery('state', '!=', '3,4,7'); //States of incident task
        getIncidentTaskRecord.query();
        if (getIncidentTaskRecord.getRowCount() >= 1) {
			current.setAbortAction(true);
            gs.addErrorMessage('Please close all the incident tasks before attempting to close this incident');
        }
    }

 

Please note that the above incident and task states are as based on OOB incident configuration, you might need to change the state conditions accordingly. 

 

Please mark Helpful / Accept Solution so that it helps others with similar questions.

Sonali Nimbalk1
Giga Guru

Hi @The_Flash,

Business rule

BR: Before Update

Condition: State Changes to Resolved

 

 

(function executeRule(current, previous /*null when async*/) {
   var incTask = new GlideRecord("incident_task");
		incTask.addQuery("incident",current.sys_id);
		incTask.addQuery("state!=3");
		incTask.query();
		if(incTask.next()){	
	                 gs.addErrorMessage(current.state+"Incident Task still not Resloved yet, please complete Incident Task first.");
			current.setAbortAction(true);
		}

})(current, previous);

 

 

Please hit like and mark my response as correct if that helps
Regards,
Sonali

Community Alums
Not applicable

Hello @The_Flash  ,

 

Business rule

BR: Before Update

Condition: State is one of Resolved, Closed, Canceled.

 

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

    var gr = new GlideRecord('incident_task');
    gr.addQuery('active''true');
    gr.addQuery('parent', current.sys_id);
    gr.query();
    if (gr.next()) {
        current.setWorkflow(false); //Use it only to prevent the message that the Incident has been resolved
        current.state = previous.state; // There are 2 Incident States and they need to be in sync. This one controls the display of the 'Close Incident' UI Action
        current.incident_state = previous.incident_state; // This is the displayed State field
        gs.addErrorMessage('Please close all the task before attempting to close this incident');
        gs.setRedirect(current);
        current.setAbortAction(true);
    }

})(current, previous);
 
@The_Flash , please mark my answer as accepted solution and helpful. If it works for u.
 

Thank you!

The_Flash
Mega Guru

My Solution:

When to Run : Before Update

Condition : State Changes to Resolved

 

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

// Add your code here

 

var taskstate = new GlideRecord('sc_task');


taskstate.addQuery('parent', current.sys_id);


taskstate.addEncodedQuery('state!=3');


taskstate.query();

 

if (taskstate.next()) {
gs.addErrorMessage("cannot resolve the incident because the task/s is not resolved");
current.setAbortAction(true);
gs.setRedirect(current);
}

})(current, previous);