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

Joni V B
Tera Guru

Hi @mcroxall ,

 

Some steps to get you going on debugging:

- confirm that your business rule is firing 

- check the system log for errors

- If your business rule fires and you spot no errors in the log, add debug statements (gs.info(...)) in your if statements to see where you get in the script.

In your first if statement, switch out current.state === x for current.getValue('state') === 'x';

GlideRecord.getValue() is safer than just getting the value from the current object and will return a String and not a number. 

 

Best regards,

Joni

vennelasai
Tera Contributor

Hello @mcroxall 

 

1. Create a after - update BR with conditions - state is one of - Closed Complete, Closed Incomplete, Closed Skipped

 

vennelasai_0-1732625156178.png

2. Apply the below script, modify fields as per your request

 

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

    var gr = new GlideRecord('sc_request');
    gr.addQuery('sys_id', current.request);
    gr.query();
    if (gr.next()) {
        var task = new GlideRecord('sc_task');
        task.addQuery('request_item', current.sys_id);
        task.query();
        var count = task.getRowCount();
        if (count == 0) {
            if (current.state == '3') {
                gr.request_state = 'closed_complete';
            }
            if (current.state == '4') {
                gr.request_state = 'closed_incomplete';
            }
            if (current.state == '7') {
                gr.request_state = 'closed_complete';
            }
            gr.update();
        }
    }
})(current, previous);

 

This code will check if current ritm has the sc task or not, if not it will update the request as per ritm state.

 

Please mark it correct/helpful If this works for you.

 

Thanks,

Vennela. 

Bert_c1
Kilo Patron

@mcroxall 

 

I tested your BR in my instance, found it works without the 'if' statement as I added the Filter condition on the "When to run" tab on my BR is "State", "changes to", "Closed Complete"; "State", "changes to", "Closed Incomplete"; "State", "changes to", "Closed Skipped. No need to duplicate that part in the script. (No Condition is present on the Advanced tab of my BR.

 

Script follows

 

 

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

	// check current record and see if the is a sc_task record that references the record
	var scTask = new GlideRecord('sc_task');
	scTask.addQuery('request_item' , current.sys_id);
	scTask.addQuery('state', 'NOT IN', '3,4,7'); // Check for any RITMs not in a closed state
	scTask.query();
	if (scTask.next()) {
		// If any sc_task is not closed, display error and abort update
		gs.addErrorMessage('There open task: ' + scTask.number + ' for this request item!');
		current.state = previous.state;
		current.setAbortAction(true);
		return;
	}

	// 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();
	// Debug, can be removed after testing.
	gs.info('CloseParentRequest: Found ' + ritmGr.getRowCount() + 
			' req item records for: ' + current.request.number);
	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
			gs.addInfoMessage('CloseParentRequest: closing: ' + req.number);
		}
	}

})(current, previous);

 

 

 

 

For the first part to work, where a check is made for open sc_task records, the BR must be defined to run 'Before' to abort the update to the sc_req_item record.

 

Hi @mcroxall 

 

Good to hear that my script helps to solve your issue.

 

Could you please mark it as helpful/correct. So that it might help others.

 

Thanks,

Vennela.