Update a field on HR profile when user record is inserted or updated.

si21
Tera Guru

Hi experts,

there is a custom field called 'Mgr' on HR profile.

whenever a manager is addedt, changed or removed to a User record. On the manger's HR profile, the custom field should be made true when added and False when manager is removed from user record.

 

How can we achieve this?

 

TIA

1 ACCEPTED SOLUTION

Instead of checking if a person is manager or not via a checkbox, you can check it via a method in a Script include using GlideAggregate. Here is the script.

function checkManager(user_sys_id){
    var managerUser = new GlideAggregate('sys_user');
    managerUser.addQuery('manager',user_sys_id);
    managerUser.addAggregate('COUNT');
    managerUser.query();
    var managerCount = 0;
    if(managerUser.next()){
        managerCount=managerUser.getAggregate('COUNT');
    }
    return managerCount;
}

gs.info(checkManager('46c6f9efa9fe198101ddf5eed9adf6e7')); //replace this with sys_id of a user 

 

If this script returns a count 0 which indicates the user is not a manager user, if it returns a count more than 0 which means user is a manager user.

 

View solution in original post

7 REPLIES 7

@si21 You need to revise the implementation slightly, whenever a person is added/updated on a user record as a manager in the manager field, you can always set the value of u_is_manager to true without even checking the count. However, whenever the manager is removed from the manager field on a record, you will find that value in previous object in the business rule.

 

Updated your BR as follows.

 

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

    if (current.manager != '' && current.manager.u_is_manager == false) {
        var glideManager = new GlideRecord('sys_user');
        if (glideManager.get(current.manager.sys_id)) {
            glideManager.setValue('u_is_manager', true);
            glideManager.setWorkflow(false);
            glideManager.update();
        }
    }
    if (previous.manager != '') {
        // Add your code here
        var managerUser = new GlideAggregate('sys_user');
        managerUser.addQuery('manager', previous.manager);
        managerUser.addAggregate('COUNT');
        managerUser.query();
        var managerCount = 0;
        if (managerUser.next()) {
            managerCount = managerUser.getAggregate('COUNT');
            if (managerCount == 0) {
                var glideOldManager = new GlideRecord('sys_user');
                if (glideOldManager.get(previous.manager.sys_id)) {
                    glideOldManager.setValue('u_is_manager', false);
                    glideOldManager.setWorkflow(false);
                    glideOldManager.update();
                }
            }
        }

    }

})(current, previous);

Screenshot 2023-08-31 at 11.47.30 AM.pngScreenshot 2023-08-31 at 11.46.51 AM.png

Hi @Sandeep Rajput , I think I was not clear in the earlier reply, this custom field 'u_is_manager' is on HR profile table [sn_hr_core_profile] not on User table. When a user is added/removed as manager, their HR profile's custom field 'u_is_manager' needs to be updated. Could you please help me. Thanks

@si21 Instead of creating the BR on sys_user, you can create in on sn_hr_core_profile and use the same logic there.