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

sushant007
Kilo Guru

Use this code in BR


var target = new GlideRecord('incident_task');


target.addQuery('active',true);


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


target.query();


if (target.next()) {


current.setAbortAction(true);


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


}



i feel code is not getting executed and thats the reason incident goes to resolved.


Lets see if it works? if not then we will put some log statements.


ohh sorry ; I hurried in understanding your issue


Looks like there must be some client script or UI policies running and setting the state to "Resolved".


can you check on the state field if it can be watched for changes happening to it? (Rt click on field and watch - State option available)


Yeah, it must be executing to show that gs.addInfoMessage, plus not saving to the database



The only script/ui policy is the OOTB UI Policy that ensures you have a close code & close notes:



Capture.JPG


Hi James,



What happens is when ever you click on resolve incident then state get set to that field as per code in OOB ui action.




And business rule is triggere after that which check for additional conditions like child task validation.



When client side code in ui action sets the field value it does not refresh it. you can use additional redirect on current form so that it will get refreshed.



Thank you,
Ashutosh