- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 10:28 PM
Hello,
I created a business rule to query the approval table for requests within my custom table.
I want the BR to put all other requested approvers except the one that approved into a "No longer Required" status.
Here is my business rule:
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 10:47 PM - edited 05-13-2025 10:49 PM
Hi @BenjaminY
Try the below script.
(function executeRule(current, previous /*null when async*/ ) {
var target_rec_id = current.document_id;
var target_rec_table = current.source_table;
// Ensure the record is from your custom table
if (target_rec_table == 'x_g_dh5_ccht_ccht_task') {
var targetGR = new GlideRecord(target_rec_table);
targetGR.get(target_rec_id);
targetGR.record_status = 'Cancelled'; //update record status field to cancelled
targetGR.update();
// 2. Set all other approvers to "not_required"
var otherApprovers = new GlideRecord('sysapproval_approver');
otherApprovers.addQuery('document_id', target_rec_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id); // Exclude current
otherApprovers.addEncodedQuery('stateINrequested,approved'); // Only update active approvers
otherApprovers.query();
while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required');
otherApprovers.setWorkflow(false);
otherApprovers.update();
}
}
})(current, previous);
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 10:41 PM
I made few changes in the script. Please see if it is working>
(function executeRule(current, previous /*null when async*/) {
if (current.state == 'cancelled') {
var targetGR = current.document_id.getRefRecord();
if (targetGR.getTableName() == 'x_g_dh5_ccht_ccht_task') {
// Update the record status field if required
targetGR.setValue('record_status', 'Cancelled');
targetGR.update();
// Cancel all other active approvals
var otherApprovers = new GlideRecord('sys_approval_approver');
otherApprovers.addQuery('document_id', current.document_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id);
otherApprovers.addQuery('state', 'IN', 'requested,approved');
otherApprovers.query();
while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required'); // Corrected line
otherApprovers.update();
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 10:47 PM - edited 05-13-2025 10:49 PM
Hi @BenjaminY
Try the below script.
(function executeRule(current, previous /*null when async*/ ) {
var target_rec_id = current.document_id;
var target_rec_table = current.source_table;
// Ensure the record is from your custom table
if (target_rec_table == 'x_g_dh5_ccht_ccht_task') {
var targetGR = new GlideRecord(target_rec_table);
targetGR.get(target_rec_id);
targetGR.record_status = 'Cancelled'; //update record status field to cancelled
targetGR.update();
// 2. Set all other approvers to "not_required"
var otherApprovers = new GlideRecord('sysapproval_approver');
otherApprovers.addQuery('document_id', target_rec_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id); // Exclude current
otherApprovers.addEncodedQuery('stateINrequested,approved'); // Only update active approvers
otherApprovers.query();
while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required');
otherApprovers.setWorkflow(false);
otherApprovers.update();
}
}
})(current, previous);
Regards,
Siva
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-23-2025 09:04 AM
Thanks this worked! is there a possible if statement I can add if none of the approvers cancelled but the request is stilled in cancelled status. I want it to make all approvers "not_required"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-13-2025 10:50 PM
(function executeRule(current, previous /*null when async*/) {
if (current.state == 'cancelled') {
var targetGR = current.document_id.getRefRecord();
if (targetGR.getTableName() == 'x_g_dh5_ccht_ccht_task') {
// Update the record status field if required
targetGR.setValue('record_status', 'Cancelled');
targetGR.update();
// Cancel all other active approvals
var otherApprovers = new GlideRecord('sys_approval_approver');
otherApprovers.addQuery('document_id', current.document_id);
otherApprovers.addQuery('sys_id', '!=', current.sys_id);
otherApprovers.addQuery('state', 'IN', 'requested,approved');
otherApprovers.query();
while (otherApprovers.next()) {
otherApprovers.setValue('state', 'not_required'); // Corrected line
otherApprovers.update();
}
}
}
})(current, previous);
I made few changes in the script. Please see if it is working