Business Rule to close Parent REQ when all RITM is closed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 08:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2024 09:45 AM - edited 11-20-2024 09:47 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2024 04:49 AM
Hello @mcroxall
1. Create a after - update BR with conditions - state is one of - Closed Complete, Closed Incomplete, Closed Skipped
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-27-2024 11:19 AM - edited 12-03-2024 01:31 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-28-2024 05:47 AM
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.