Prevent PRB closure when active children

shane_davis
Tera Expert

My goal is to prevent Problem tickets from closing when there are active Problem Tasks OR Change Requests.   I have the business rule below and it is aborting the submission, but it aborts even if all children are closed.

I also tried changing rec.addQuery('problem', current.sys_id); to rec.addQuery('parent', current.sys_id); but that only prevented closure on Change Requests and not Problem Tasks...which really didn't make sense.

Business Rule:

Table: Problem

Condition: current.problem_state == 4       This is closed.

When:   Before --> Update

//Query for associated active tasks

//Should be run as a 'before' business rule on the planned_task table

var rec = new GlideRecord('task');

rec.addQuery('problem', current.sys_id);

rec.addQuery('active', true);

rec.query();

//If any of the tasks are active abort the submission

if(rec.hasNext()){

      gs.addInfoMessage('Submission aborted due to active child tasks.');

      current.setAbortAction(true);

}

9 REPLIES 9

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

HI Shane,



Replace the first line as


var rec = new GlideRecord('problem_task');  




Please let me know if you have any questions.


Pradeep,



        Thank you for your reply.   I can get the rule working if I only want to check for active problem tasks.   However, I need to check for active problem tasks & change requests.



Shane


Here is code that we use off the task table: The GlideRecord query looks for any tasks that are parent to the current task being updated.



// Business Rule Condition: current.active.changesTo(false)
preventInactivation();



function preventInactivation() {


  var child = new GlideRecord('task');


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


  child.addQuery('sys_class_name','!=','sysapproval_group');


  child.addActiveQuery();


  child.query();



  var count = 0;


  var childLink = '';



  //gs.log('Aggregate Count: ' + child.getRowCount());



  if(child.hasNext()) {


  while(child.next()) {


  childLink = '<a href="https://' + gs.getProperty('instance_name') + '.service-now.com/nav_to.do?uri=' + child.getTableName() + '.do?sys_id=' + child.sys_id + '" target="blank">' + child.number + '</a>';


  gs.addErrorMessage('' + childLink + ' is an active child task to ' + current.number + ' and must be <b>Closed</b> or <b>Cancelled</b> first.');



  count++;


  }



  current.state = previous.state;


  current.active = previous.active;



  gs.addErrorMessage(current.number + ' still has ' + count + ' active children tasks. Please have these tasks <b>Closed</b> or <b>Cancelled</b> first.');



  current.setAbortAction(true);



  return false;


  }


}



The error messages may or may not be overkill for you, but it works for us...



If you're looking specifically for Problem Tasks or Change Requests, you may need 2 separate GlideRecord queries referencing the 2 different tables.


For us, because all tasks are under the tasks table, it catches any task that is parent to the current task (in your case a Problem record) that is being updated.



If you put this on the Problem table, and you want to specifically look for change requests, you have to find the reference field on the Change form that references back to the Problem Record.


Daniel,



        Thank you.   I am going to try this.   If I am looking at this right, does it mean that you are not able to close any ticket that has any active children where the tables extend the Task table?



Shane