Set Approval status to approved only when all the 3 or 4 or 5 level of approvers approves the RITM

raj99918
Tera Contributor

Hi 

 

I have requirement like to set the Approval status to approved only(1st level of approver is manager and 2nd level ABC group approvals and 3rd level XYX Group approval) if all these 3 level of approvers approves the RITM then the Approval status should turn to approved How can I achieve it from Business Rule.

 

Thanks

12 REPLIES 12

Community Alums
Not applicable

Hi @raj99918 ,

yes, We can use the following code in After business rule:

Script:

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

// Check if all 3 levels of approvers have approved

var approval1 = new GlideRecord('sys_approval_approver');

approval1.addQuery('document_id', current.sys_id);

approval1.addQuery('approver', current.manager); // 1st level approver (Manager)

approval1.addQuery('state', 'approved'); // State should be approved

approval1.query();

 

var approval2 = new GlideRecord('sys_approval_approver');

approval2.addQuery('document_id', current.sys_id);

approval2.addQuery('approver', 'IN', 'ABC Group'); // 2nd level approvers (ABC Group)

approval2.addQuery('state', 'approved'); // State should be approved

approval2.query();

 

var approval3 = new GlideRecord('sys_approval_approver');

approval3.addQuery('document_id', current.sys_id);

approval3.addQuery('approver', 'IN', 'XYZ Group'); // 3rd level approvers (XYZ Group)

approval3.addQuery('state', 'approved'); // State should be approved

approval3.query(); // Check if all 3 approval queries return records

 

if (approval1.getRowCount() > 0 && approval2.getRowCount() > 0 && approval3.getRowCount() > 0) { // If all approvers have approved, set the Approval Status to 'Approved'

current.approval_status = 'approved'; // Assuming you have a field for approval_status

current.update();

}

})(current, previous);

 

Hi @Community Alums Thanks for the reply but here number of level of approvers we dont know because I need to implement this for all the catalog forms in my system every catalog form have their own approval flow right may be having 1 level of approvals some have 2 level of approvals in this case How will we write the generic Business rule ?

Community Alums
Not applicable

Hi @raj99918 The following script helps to approval status should only be updated to "Approved" when all approvers have approved the RITM. You may can a query on the same for approval whether is belongs to group or NOT.

 

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

// We need to check if this approval state has changed to 'approved'
if (current.state != 'approved') {
return;
}

// Get the related RITM record
var ritm = new GlideRecord('sc_req_item');
if (!ritm.get(current.document_id)) {
return; // Exit if RITM doesn't exist
}

// Check if all approvers have approved for the current RITM
var allApproved = true;

// Query all approvals associated with this RITM
var approvers = new GlideRecord('sys_approval_approver');
approvers.addQuery('document_id', ritm.sys_id);
approvers.addQuery('state', '!=', 'approved'); // Find all non-approved approvals 
approvers.query();

 

// If there are any non-approved approvers, set allApproved to false
if (approvers.getRowCount() > 0) {
allApproved = false;
}

// If all approvals are in 'approved' state, update the RITM's approval status to 'approved'
if (allApproved) {
ritm.approval_status = 'approved';
ritm.update(); // Update the RITM with 'approved' status
}

})(current, previous);

 

Hi @Community Alums Thanks for the script but still it's setting the approval status to approved and reset to requested state again even though 2nd level of approval is pending and am attaching the worknotes snap for the same. I have written this BR on Sc_req_item table after update Am I right ?

 

ss.jpg