How to close parent when all children are closed?

JL5
Tera Contributor

I am attempting to write a client script to cover all artifacts in the task table.

Upon change to state to any of the closed states (I believe out of the box they are 3, 4, 7, 8, 5, 6, 106, 107, 157), have the script check the parent to the child and list records with states that open. If there are more than zero do nothing, if less than zero then close the parent.

Is this possible? I was hoping to modify this script that I found from https://community.servicenow.com/community?id=community_question&sys_id=24848d6cdb77cc942be0a851ca961918.

 

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

// Add your code here
var wo = new GlideRecord("u_navision_sales_order");
wo.addQuery("u_request_item", current.u_request_item);
wo.addQuery("u_status", '!=', <<status value>>); //replace with completed value
wo.orderBy('sys_created_on');
wo.query();
if (wo.next()) {
//Don't close the parent record as one of the child is not completed .

gs.addInfoMessage('Parent can't be closed because one of the child is in Open');
}

else
{

var req = new GlideRecord("sc_req_item");
req.addQuery("number", current.u_request_item);
req.addQuery('state', 'IN', '1 , 2');
req.query();
if (req.next()) {
req.state = '3';
req.update();
}
}
})(current, previous);

 

I'm thinking something like this might work?

 

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

// Add your code here
var wo = new GlideRecord("task");
wo.addQuery("task", current.task);
wo.addQuery("state", '!=', '3 , 4 , 7 , 8 , 5 , 6 , 106 , 107 , 157'); //replace with completed value
wo.orderBy('sys_created_on');
wo.query();
if (wo.next()) {
//Don't close the parent record as one of the child is not completed .

gs.addInfoMessage('Parent cannot be closed because one of the child is in Open');
}

else
{

var req = new GlideRecord("task");
req.addQuery("number", current.task);
req.addQuery('state', 'IN', '1 , 2 , -5 , -4 , -3 , -2 , -1 , 0 , 101 , 102 , 103 , 104 , 151 , 152 , 154 , 9');
req.query();
if (req.next()) {
req.state = '3';
req.update();
}
}
})(current, previous);
2 REPLIES 2

Allen Andreas
Administrator
Administrator

Hello,

Please use the appropriate forum feature "Insert/Edit code sample" when pasting code as it keeps things organized and easier to read

find_real_file.png

As far as your post, it's not very clear if you're saying the script works or not or if you're wanting to check and see "should" you do it, etc.

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Oh thank you, I did not see that function when I posted. I have edited the code sample.

The script in its current state does not work for me as it is tailored to the other person's solution. I am attempting to see if what I am asking is even possible and if so, maybe some direction on how I could change that script (or if I should scrap that script entirely). My apologies that I wasn't clearer in the original post and thank you for your time in responding!