Business Rule script that will check if all problem task is close

mark141230
Tera Expert

Hi Everyone,

is there a way for me to check if all problem task associated to a Problem is already close then once close it will change the problem state.

It is easy if I can do it via Workflow but the problem task is not consistent in number.

I think you can create it in a business rule but don't know how.

I tried to run this in the backgroud script to see if its working but to no avail.

var gr = new GlideRecord('problem_task');

gr.addQuery('problem', 'IN', 'PRB0100145');

gr.addquery('u_problem_task_state', '1');

gr.query();

while (gr.next()) {

gs.print(gr.number);

}

need your help please.

8 REPLIES 8

amlanpal
Kilo Sage

Hi Mark,



For this you need to write a Business Rule. Please find the below screenshots. This is working fine in my instance. Please let me know if you have any issue. For your simplicity I'm providing the code below:



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



  var gr = new GlideRecord('problem_task');


  gr.addActiveQuery();


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


  gr.query();


  if (gr.next()) {


  gs.addInfoMessage('Please close the related Problem tasks first.');


  current.setAbortAction(true);


  }


})(current, previous);



find_real_file.png


find_real_file.png



I hope this helps.Please mark correct/helpful based on impact


snehabinani26
Tera Guru

HI Mark,,


Please try this after BR in problem task table with condition state changes to closed.



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



  //If all   Problem Tasks are closed, automatically move the Problem closed State




  var gr = new GlideRecord("problem_task");


  gr.addQuery("problem", current.problem.sys_id);


  gr.addquery("sys_id", "!=", current.sys_id);


  gr.addActiveQuery();


  gr.query();



  if (!gr.hasNext()) {


  var prbRec = new GlideRecord("problem");


  prbRec.get(current.problem.sys_id);


  prbRec.state = 3; // check the closed state value


  prbRec.work_notes = "Problem automatically moved to Closed state as all Problem Tasks have been closed"


  prbRec.update();


  }



})(current, previous);



Hope this helps you.


BALAJI40
Mega Sage

try this,


write a BR


var gr = new GlideRecord('problem_task');


gr.addQuery('problem',current.sys_id;


gr.addquery('state!=4'); //state is not closed or resolved


gr.query();


if(gr.hasNext()){


gs.addInfoMessage('PLease closed related problem tasks');


current.setAbortAction(true);


}



untested, please copy this code and check


mark141230
Tera Expert

How about a BR script that will check if the assignment group is not empty. once not empty it will move the status.



thanks again