Business Rule or similar to update the manager field when a user logs in

wiganmichae
Tera Contributor

Hi,

I have an issue where when a user logs in I want the system to change the manager to a set person if the manager field is either empty or the manager in there is either inactive or locked out.

I have an async business rule set to run after update and run the below script but it doesn't seem to be triggering or working as expected.

 

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


    var currentUserID = currentUser.getID(); 
    var user = new GlideRecord('sys_user');

    if (user.get(currentUserID)) {
        var managerID = user.manager;
        var manager = new GlideRecord('sys_user');

        // Check if manager is empty
        if (!managerID) {
            jslog('No manager assigned. Changing manager for user: ' + user.user_name);
            user.manager = '7c7b45b31bfa7910bc98eb59b04bcb76'; // Replace with the new manager's sys_id
            user.update();
            return;
        }

        // Check if the current manager is active or not locked out
        if (manager.get(managerID)) {
            if (!manager.active || manager.locked_out) {
                jslog('Manager is inactive or locked out. Changing manager for user: ' + user.user_name);
                user.manager = '7c7b45b31bfa7910bc98eb59b04bcb76'; // Replace with the new manager's sys_id
                user.update();
            }
        }
    }

    function jslog(message) {
        gs.info('Business Rule: Update Manager on Login - ' + message);
    }

})(current, previous);

 

 

Any help to get this working would be greatly appreciated. 

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

@wiganmichae 

that's not how manager are populated in sys_user.

You don't have AD/LDAP to bring in users.

also you want the manager to be updated when user logins so will that business rule run when user logs in?

try this and it will work if BR condition satisfies

Make BR as Before Update and Below Condition

Last Login time [Changes]

(function execute(inputs, outputs) {
    var userId = gs.getUserID();
    if (!userId)
        return;

    var user = new GlideRecord('sys_user');
    if (!user.get(userId))
        return;

    var managerId = user.manager.sys_id; // or user.getValue('manager')
    var updateNeeded = false;
    var newManagerSysId = '7c7b45b31bfa7910bc98eb59b04bcb76'; // target manager sys_id

    if (!managerId) {
        updateNeeded = true;
        gs.info('No manager assigned. Updating manager for user: ' + user.user_name);
    } else {
        var manager = new GlideRecord('sys_user');
        if (manager.get(managerId)) {
            if (!manager.active || manager.locked_out) {
                updateNeeded = true;
                gs.info('Manager inactive/locked. Updating manager for user: ' + user.user_name);
            }
        }
    }

    if (updateNeeded) {
        current.manager = newManagerSysId;
    }
})(inputs, outputs);

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader