Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Business Rule to close Parent REQ when all RITM is closed

mcroxall
Tera Guru
I was wondering if you could help me with a script for a business rule i am working on.
 
The parent REQ are not auto closing when the RITM are closed when there is no task within them. I have reached out to SN, but they are saying this is not OOB, so they can't help.
 
for same reason is not working i can't seem to find anything else that is preventing that BR to execute or if my script is wrong.
 
 
 
My business rule is as follows:
Table: sc_req_item
When to run: after update
condition: when the state is one of closed complete, closed incomplete and closed skipped
 
Script:
(function executeRule(current, previous /*null when async*/) {
 
    // Ensure this runs only when the state is updated
    if (current.state.changes() && (current.state == 3 || current.state == 4 || current.state == 7)) {
        // Check if all RITMs for the same Request (sc_request) are closed
        var allRITMsClosed = true; // Flag to determine if all RITMs are closed
        var ritmGr = new GlideRecord('sc_req_item');
        ritmGr.addQuery('request', current.request); // Filter by the parent request
        ritmGr.addQuery('state', 'NOT IN', '3,4,7'); // Check for any RITMs not in a closed state
        ritmGr.query();
       
        if (ritmGr.hasNext()) {
            allRITMsClosed = false; // If any RITM is not closed, set the flag to false
        }
 
        // If all RITMs are closed, update the parent request to match the current RITM's state
        if (allRITMsClosed) {
            var req = current.request.getRefRecord(); // Get the parent request record
            if (req) {
                req.state = current.state; // Set the request state to match the RITM state
                req.update(); // Update the request record
            }
        }
    }
 
})(current, previous);
6 REPLIES 6

Choice values stored in the database for the 3 states are '3', '4', '5'. Not the 'label' value.

Hi,

 

Yes, I agree. But in RITM I see the values you have mentioned(3,4,5 ...). But in request I have taken a reference from my instance, So based on the respected instances there might be changes.