script help with a business rule to prevent closing a problem if there are open child tasks

patricklatella
Mega Sage

hi all,

I've build a couple similar business rules but am struggling with this one.   I have a related list of child tasks on our Problem records ([problem_task] table).

And what I'm trying to do is use "after" business rule on the [Problem] table to query the "problem_task" table to see if there are associated tasks, and then check their "state" field, and if the state on the child tasks is not one of the 3 complete states, to abort the closing of the parent Problem record.   Here is my script with comment notes.   thanks!

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

var prbNumber = current.number; //Problem number

gs.info('++++prbNumber1 is '+prbNumber);//this returns correctly

var gr = new GlideRecord('problem_task'); // This will be the child task table

gr.addQuery('problem',prbNumber); //field on the child table where the parent record number is = "problem"

gr.query();

gs.info('++++prbNumber2 is '+gr.number);//this returns undefined

if(gr.next()){

gs.info('++++state is '+gr.state);//this is not reached even if there is a record on the problem_task table with the "problem" field matching the prbNumber

if (gr.state == '3' || gr.state == '4' || gr.state == '7'){

 

  gs.addErrorMessage('Submission aborted, all associated tasks must be closed before this Problem record can be closed.');

//current.setAbortAction(true);

//current.state = previous.state;

}

}

})(current, previous);

1 ACCEPTED SOLUTION

Goran WitchDoc
ServiceNow Employee
ServiceNow Employee

Hi Patrick,



Without checking to deep into the code, I would say this line is the problem:



gr.addQuery('problem',prbNumber); //field on the child table where the parent record number is = "problem"



The problem field contains the sys_id of the problem, not the problem number



I would do something like this:



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





var prb = current.getUniqueValue(); //get sys_id


var gr = new GlideRecord('problem_task'); // This will be the child task table


gr.addQuery('problem',prb); //field on the child table where the parent record number is = "problem"


gr.addActiveQuery();/Only want to look if there is active tasks


gr.setLimit(1);//Set limit to 1 since it only require 1 active task to abort


gr.query();




if(gr.next()){






  gs.addErrorMessage('Submission aborted, all associated tasks must be closed before this Problem record can be closed.');




current.setAbortAction(true);


}




})(current, previous);



View solution in original post

8 REPLIES 8

Prateek kumar
Mega Sage

This thread has your answer. Make modifications accordingly.


Close RITM if task is set to cancelled



Please mark my response as correct and helpful if it helped solved your question.
-Thanks

juancarvajal
Tera Contributor

Is the "problem" field on the "problem_task" table a reference? If it is you have to change the query. You can use gr.addQuery('problem',current.sys_id); or if you already have the number use dot walk on the query. gr.addQuery('problem.number', prbNumber);
Thats the reason why it doesn't enter on the if(gr.next()) . Because there are not records using that filter.


Remember not to dot-walk like problem.number. That can be a major performance issue since it demands another server-call for each record it get.


Really?
I didn't know that. So it's better to not use dot-walk on queries?