The CreatorCon Call for Content is officially open! Get started here.

Prevent Incident Resolved if child task active (Jakarta)

James Blight
Kilo Sage

Hi all,

I know this question has been asked several times and is covered on the wiki, but 'How to prevent an incident record being resolved or closed if a child task is still open'.

Sure I have done this fine in the past, and reread the below links:

http://wiki.servicenow.com/index.php?title=Prevent_Closure_if_Child_Task_is_Active#gsc.tab=0

how can i made related list field like if state of change task is closed then only one can close the...

https://community.servicenow.com/thread/279369

Prevent Incident Resolution When Tasks Are Still Open

Preventing Resolve or Close when Incident or Change Request has open Tasks

https://www.servicenowguru.com/scripting/stopping-record-submission-servicenow/

Don't close incidents when incident tasks are open

Which all use the same method of doing a glide over the child table using the parent (current) sys_id to check for active=true records, then using setAbortAction.

Code from my own 'before update' BR below, condition 'State changes to Resolved', order 100:

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

var target = new GlideRecord('incident_task');

target.addQuery('active','true');

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

target.query();

if (target.next()) {

current.setWorkflow(false);

gs.addInfoMessage('Please close child task');

current.setAbortAction(true);

}

})(current, previous);

Except, in the customer instance, and my own OOTB local dev instance the state of the incident on the form still shows as Resolved to the user (but has not been saved to the database), and the close ui action is usable:

Capture.JPG

So I thought add a line of current.state = previous.state; in there, so at least the state is back to how it was before and although that fixes the state showing as resolved issue, the 'Close Incident' UI action is still there (extra bonus on the customer instance, the OOTB 'Caller Close' BR is also firing showing the message 'Incident xxx has been resolved).

Another idea was to add a line of gs.redirect(current); to force the form to reload, but that seemed to have no effect.

Then I found out there is a known error about using it in this way: ServiceNow KB: gs.setRedirect() in combination with current.setAbortAction(true) not executed (KB053...

My question is - has this changed in Jakarta so it no longer works? As on other threads I can see people using the exact same code for the same use case with no issue.

Or is there an alternative method?

Thanks

James

1 ACCEPTED SOLUTION

Hi Ashutosh,

Just saw this is still open when logged in for something else.

I think towards the end of this thread I had raised it with ServiceNow, mainly so they update the example in the docs.

They replied back advising to use the below in a before BR, which worked:

(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()) {
   gs.addInfoMessage('This Incident cannot be resolved as there is or are active child Incident Tasks. Please close them first');
   //current.setWorkflow(false); >>> Use it only to prevent the message that the Incident has been resolved
   current.incident_state = previous.incident_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.state = previous.state; // This is the displayed State field
   current.setAbortAction(true);
   gs.setRedirect(current);
}

})(current, previous);

 

I think either your example, or something I had done had worked before getting this reply anyway but posted here for anyone else who may find useful

Thanks

 

James

View solution in original post

22 REPLIES 22

James,



I was working with the script and below script worked for me:



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


var target = new GlideRecord('incident_task');


target.addQuery('active','true');


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


target.query();


if (target.next())


{


current.setWorkflow(false);


current.state = previous.state;


gs.addInfoMessage('Please close child task');


gs.setRedirect(current);


current.setAbortAction(true);


} })(current, previous);



Thank you,


Ashutosh



Please Hit Correct, Helpful or like,if you are satisfied with this response.


Did it actually redirect?


Or did it stay on the form & can see the 'Close Incident' UI action at the top?


Which version of ServiceNow are you on, Jakarta?


HI James,



This has marked the state to previous state. Now I am on Jakarta.



Close incident is still Visible, working on it.



Thank you,
Ashutosh Munot      



Please Hit Correct, Helpful or Like


HI James,



See below code, Now this has done all what you want. We need to map incident state as well to hide Close Incident Button.



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


var target = new GlideRecord('incident_task');


target.addQuery('active','true');


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


target.query();


if (target.next())


{


current.setWorkflow(false);


current.state = previous.state;


current.incident_state = previous.incident_state;


gs.addInfoMessage('Please close child task');


gs.setRedirect(current);


current.setAbortAction(true);


} })(current, previous);



Thank you,


Ashutosh munot



Please Hit Correct, Helpful or like,if you are satisfied with this response.


It works Thanks