Use PDIs? Take our 5-minute survey to help shape the PDI roadmap.

Redirect approval to users manager if user goes inactive

Ayush1
Kilo Contributor

Hi All, 

I am writing a Business rule (after/update) if any user goes inactive then it script will assign approval records to users manager.

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

var gr=new GlideRecord('sysapproval_approver');
gr.addEncodedQuery('state=requested^approver.sys_id=',+current.sys_id);
gr.query();
while(gr.next())
{

gr.approver=current.manager;
gr.update();

}

})(current, previous);

 

but this script is not working. Kindly suggest

3 REPLIES 3

bammar
Kilo Sage

Your script may have a synytax error somewhere but what about a different approach...

I think the problem is if the approval has already been sent- it has to be approved from that exact user or else it wont work unless the Manager is a Delegate. - You could make a Business rule that when a user is marked inactive- then their manager is set as a delegate instead - So in the conditions- Active marked as false- then  create a new Delegate record and plug in the values for the user and their manager. and check Approvals box. 

So a request comes in where User A will be an approver

User A before they get a chance to approve is marked inactive- the script will make their manager the delegate - the manager can now approve under MY approvals

IF the user is marked inactive before the workflow reaches user A- the manager will get the email

If it is after they will have to go in and approve under MY PENDING APPROVALS- but this is simular to unnassigning tickets from an ex employee you may clear or process their pending approvals 

Chandra2
Tera Contributor

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

var approverGR = new GlideRecord('sys_user');
if (approverGR.get(current.approver)) {

// 1. Check if the approver's manager is valid and active
var managerID = approverGR.manager.toString();

if (managerID) {
var managerGR = new GlideRecord('sys_user');
if (managerGR.get(managerID) && managerGR.active == true) {
// Route the approval to the active manager
current.approver = managerID;
return;
}
}

// 2. If no active manager is found, set the approval to No Longer Required
current.state = 'no_longer_required';
current.comments = 'Approval was automatically skipped because the original approver and their manager are inactive.';
}

})(current, previous);

 

 

Please mark helpful if the above code fix your issue.

ajmalmuhamm
Tera Contributor

The issue is with your encoded query. You're concatenating the current.sys_id incorrectly. Instead, query the approver reference field directly:

(function executeRule(current, previous) {

    if (!current.manager)
        return;

    var gr = new GlideRecord('sysapproval_approver');
    gr.addQuery('state', 'requested');
    gr.addQuery('approver', current.sys_id);
    gr.query();

    while (gr.next()) {
        gr.approver = current.manager;
        gr.update();
    }

})(current, previous);

Also ensure:

  • The Business Rule runs After Update.
  • The rule only executes when the Active field changes to false (e.g., current.active.changesTo(false)).
  • The inactive user has a Manager value populated.
  • The Business Rule has permission to update sysapproval_approver records.