Add users to group when someone is added to On Call roster

pramodkumar
Tera Expert

Hi All,

I have a requirement when someone is added to a on call roster, a custom role should be added to user and if we remove user from roster the role should be removed.  Can we do this by adding/removing user from group which contains the custom role. Is this possible?

 

Thanks!

4 REPLIES 4

Neeraj Modi
Tera Guru

Hi @pramodkumar 

 

The stated requirement is possible to implement.

To help others quickly find solution, please mark this answer as "Helpful" or "Accept Solution"  If it solved your problem/Question.

Zack Hilacan1
Mega Sage

Hello @pramodkumar ,

 

You can achieve this through Business rule.

Be sure to use the table cmn_rota_member then in your when to run use "before " . "Delete".

ZackHilacan1_0-1752557341805.png

So that you can still access the data such as the Group and the ID of the member you are going to remove.

Then you can do advanced scripting to navigate to the Group table and find that group where your role is the remove the user.

 

If this has been of any help give a kudos or mark as solution, this way others with the same scenario will be able to solve their concern in no time.😀

 

Good luck!

 

Shraddha Kadam
Mega Sage

Hello @pramodkumar ,

 

Follow below steps -

Create Business rule with below steps

Name: Add Custom Role to On-Call Member

Table: cmn_rota_member

When to run: After Insert

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

    var userId = current.member.sys_id; // Sys_id of the user being added
    var roleName = 'your_custom_role'; // Name of the custom role you want to add

    // Get the sys_id of the custom role
    var grRole = new GlideRecord('sys_user_role');
    grRole.addQuery('name', roleName);
    grRole.query();
    if (grRole.next()) {
        var roleSysId = grRole.sys_id;

        // Check if the user already has the role to prevent duplicates
        var grUserRole = new GlideRecord('sys_user_has_role');
        grUserRole.addQuery('user', userId);
        grUserRole.addQuery('role', roleSysId);
        grUserRole.query();

        if (!grUserRole.next()) {
            // Add the role to the user
            var grNewUserRole = new GlideRecord('sys_user_has_role');
            grNewUserRole.initialize();
            grNewUserRole.user = userId;
            grNewUserRole.role = roleSysId;
            grNewUserRole.insert();
            gs.info('Added role ' + roleName + ' to user ' + current.member.name + ' due to on-call roster addition.');
        } else {
            gs.info('User ' + current.member.name + ' already has role ' + roleName + '.');
        }
    } else {
        gs.error('Custom role "' + roleName + '" not found. Please verify role name.');
    }

})(current, previous);
If my response was helpful, please mark it as correct and helpful.
Thank you.

Hello @pramodkumar ,

 

If my answer is helpful, please accept it to close this thread for future reference.

If my response was helpful, please mark it as correct and helpful.
Thank you.