how to close child ritm when approver has been cancelled from parent ritm
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 09:36 PM
hi request to some one please help me with code correction for the requirement is that when parent approver has been cancelled, the child ritm also need to be cancelled and the child approver need to be cancelled when the parent ritm has been cancelled through approver
written after update business rule on sys approver table and the code as below :
if (current.approval == 'rejected') {
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('parent', current.sys_id);
childRITMs.query();
(childRITMs.next())
childRITMs.state = 4;
childRITMs.update();
var approverGR = new GlideRecord('sysapproval_approver'); approverGR.addQuery('document_id', childRITMs.sys_id); //
approverGR.addQuery('state', 'requested');
approverGR.query();
while (approverGR.next()) { approverGR.state = 'rejected';
approverGR.update();
} } })(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:05 PM
Hi @raj765_32 , Please try this script -
if(current.state == 'rejected'){
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('parent', current.sysapproval);
childRITMs.query();
if (childRITMs.next()) {
childRITMs.state = 4;
childRITMs.update();
}
var approverGR = new GlideRecord('sysapproval_approver');
approverGR.addQuery('sysapproval', childRITMs.sys_id); //
approverGR.addQuery('state', 'requested');
approverGR.query();
while (approverGR.next()) {
approverGR.state = 'rejected';
approverGR.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:07 PM
Explanation -
if(current.state == 'rejected'){ //instead of current.approval - current.state
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('parent', current.sysapproval); //instead of current.sys_id - current.sysapproval
childRITMs.query();
if (childRITMs.next()) {
childRITMs.state = 4;
childRITMs.update();
}
var approverGR = new GlideRecord('sysapproval_approver');
approverGR.addQuery('sysapproval', childRITMs.sys_id); //instead of document_id - sysapproval
approverGR.addQuery('state', 'requested');
approverGR.query();
while (approverGR.next()) {
approverGR.state = 'rejected';
approverGR.update();
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:19 PM - edited 03-03-2025 10:22 PM
Try the below:
after update business rule on [sysapproval_approver] table
condition: state change to rejected
Script:
(function executeRule(current, previous /*null when async*/ ) {
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('parent', current.document_id);
childRITMs.setLimit(1);
childRITMs.query();
if (childRITMs.next()) {
childRITMs.state = '4';
childRITMs.update();
}
//When RITM is set to 'Closed Incomplete' [4] then the associated 'Requested' approvals are set to 'No longer Required'
// var approverGR = new GlideRecord('sysapproval_approver');
// approverGR.addQuery('sysapproval', childRITMs.sys_id); //
// approverGR.addQuery('state', 'requested');
// approverGR.query();
// while (approverGR.next()) {
// approverGR.state = 'rejected';
// approverGR.comments = current.comments.getJournalEntry(1);
// approverGR.update();
// }
})(current, previous);
Mark this as Helpful / Accept the Solution if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-03-2025 10:55 PM
Hi @raj765_32
Your code looks fine but need following changes to see expected result:
- The while loop is incorrectly using childRITMs.next() - The way this is written means the script will only process the first childRITMs record found, instead of all child RITMs.
- update() inside a while loop - This is being done in a way that could cause issues if there are multiple child RITMs and approvers. We should make sure to handle updates properly.
- Missing proper handling of child approvers: The script should ensure that the child approvers are only updated after processing all related child RITMs.
Try bellow updated script:
if (current.approval == 'rejected') {
// Get the child RITMs based on the parent RITM (current record)
var childRITMs = new GlideRecord('sc_req_item');
childRITMs.addQuery('parent', current.sys_id); // Query for child RITMs
childRITMs.query();
// Iterate over all the child RITMs
while (childRITMs.next()) {
// Set the state of the child RITM to 'Cancelled' (state = 4)
childRITMs.state = 4;
childRITMs.update(); // Update the child RITM state to Cancelled
// Find and cancel the child approvers (set to 'rejected' state)
var approverGR = new GlideRecord('sysapproval_approver');
approverGR.addQuery('document_id', childRITMs.sys_id); // Find approvers for the child RITM
approverGR.addQuery('state', 'requested'); // Filter approvers who are in 'requested' state
approverGR.query();
// Iterate over all child approvers and set their state to 'rejected'
while (approverGR.next()) {
approverGR.state = 'rejected'; // Set approver's state to 'rejected'
approverGR.update(); // Update the approver's record
}
}
}
I hope my answer helps you to resolve your issue, if yes please mark my answer helpful and correct.
thank you
Rajesh