Restrict change closure if there are open tasks

ramkrish
Kilo Contributor

Hi,

I have coded as per below in an before update BR.

var task=new GlideRecord('Change_Task');

  task.addJoinQuery('change', current.number,'change_task.number');

  task.query();

  while (task.hasnext())

  {

  if (task.state != 'Closed Complete')

  gs.addErrorMessage('Ticket cannot be moved to Closed');

  }

But it is not working. Can you help here?

10 REPLIES 10

amlanpal
Kilo Sage

Hi Rama,



You need to write a before Update Business rule on Change Request (change_request) table to achieve your requirement. Please find the script and screenshots below.



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


  var gr = new GlideRecord('change_task');


  gr.addActiveQuery();


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


  gr.query();


  if (gr.next()) {


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


  current.setAbortAction(true);


  }


  else{


  gs.addInfoMessage('You have Successfully Closed the Change Request');


  }


})(current, previous);



find_real_file.png


find_real_file.png




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


Hi ramkrish,



If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.


If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View


Thanks Amlan for the reply.


In the above code the condition is not mentioned to check whether the task is open or not.@


Hi Rama,



Please check the line number 4 where I have check whether the Tasks are Active or not. By default, whenever any task is closed it is marked as Inactive. So automatically line number 4 will filter only those Change tasks which are active. I believe this is what we need to check.



Still, if you explicitly want to query with the State of the Change task, then please add the below line after line number 4:


gr.addQuery('state', '3'); //Check the 'Closed' state value is '3'



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