How to close parent record when all child records are completed

mkdj12
Giga Contributor

I'm using the script below to query all associated child work order records by the parent request item record. My goal is to automatically close the request item once all associated records are closed. The problem I'm running into is that the request item is closing as soon as you close one child record. Would anyone have any recommendations on how I can fix this business rule: 

Conditions: Status 'Changes To' Completed. 

(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.orderBy('sys_created_on');
    wo.query();
    while (wo.next()) {
        if (wo.u_status != 1 || wo.u_status != 2 || wo.u_status != 3 || wo.u_status != 4 || wo.u_status != 6) {
            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);
7 REPLIES 7

Thanks Suseela,

Unfortunately this is changing the parent ritm when only one of the child tasks are closed. I wanted the parent ritm to only close when all of the child tasks are closed

Have you updated the below line with the completed status value

wo.addQuery("u_status", '!=', <<status value>>); //replace with completed value

Or use encodedQuery to check the opened child records.

wo.addEncodedQuery("u_status!=<<status value>>");// update the completed status value

 

If I have answered your question, please mark my response as correct and/or helpful.

Thanks,

Suseela P.

Pooja Yadav3
Mega Expert

Hi mkdj12,

you can go through the following link :

https://community.servicenow.com/community?id=community_question&sys_id=9d15836ddbd8dbc01dcaf3231f96...

Let me know if I have helped you.

 

Hope this helps you.

Regards,

Pooja Yadav