Add/remove role from a user

Rosy14
Tera Guru

Hi, I have a RP. there is a field xyz. When i add a user to this field a role abc need to add and if i remove the user then it the role is removed. I have checked on of the code in community. Removing part ids not working.

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_buying_assistant != previous.u_buying_assistant) {
        var userRole = new GlideAggregate('sys_user_has_role');
        userRole.addQuery('user', current.u_buying_assistant); //replace with your 
        userRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41'); //Add your role sys_id here
        userRole.addAggregate('COUNT');
        userRole.query();
        if (userRole.next()) {
            var roleCount = 0;
            roleCount = userRole.getAggregate('COUNT');
            gs.addInfoMessage(roleCount);
            if (roleCount == 0) { //Only add role if the user doesn't have it
                var glideRole = new GlideRecord('sys_user_has_role');
                glideRole.initialize();
                glideRole.user = current.u_buying_assistant;
                glideRole.role = '2def524a1bfffd100f52a797b04bcb41';
                glideRole.state = 'active';
                glideRole.insert();
			}
                if (previous.u_buying_assistant != '') { //Code to remove the role from previous owner.
                    var removeRole = new GlideRecord('sys_user_has_role');
                    removeRole.addQuery('user', previous.u_buying_assistant);
                    removeRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    //remove.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRole.next()) {
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRole.deleteRecord();
                    }
                }

            }
        }
		
})(current, previous);
1 ACCEPTED SOLUTION

Sandeep Rajput
Tera Patron
Tera Patron

@Rosy14 Please update your script as follows and it will work on remove too.

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_buying_assistant != previous.u_buying_assistant) {
        var userRole = new GlideAggregate('sys_user_has_role');
        userRole.addQuery('user', current.u_buying_assistant); //replace with your 
        userRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41'); //Add your role sys_id here
        userRole.addAggregate('COUNT');
        userRole.query();
        if (userRole.next()) {
            var roleCount = 0;
            roleCount = userRole.getAggregate('COUNT');
            gs.addInfoMessage(roleCount);
            if (roleCount == 0) { //Only add role if the user doesn't have it
                var glideRole = new GlideRecord('sys_user_has_role');
                glideRole.initialize();
                glideRole.user = current.u_buying_assistant;
                glideRole.role = '2def524a1bfffd100f52a797b04bcb41';
                glideRole.state = 'active';
                glideRole.insert();
			}
                if (previous.u_buying_assistant != '') { //Code to remove the role from previous owner.
                    var removeRole = new GlideRecord('sys_user_has_role');
                    removeRole.addQuery('user', previous.u_buying_assistant);
                    removeRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    removeRole.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRole.next()) {
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRole.deleteRecord();
                    }
                }

            }
        }
		
})(current, previous);

Hope this helps.

View solution in original post

4 REPLIES 4

Jaspal Singh
Mega Patron
Mega Patron

Hi,

I believe you might need to set the inherited to 0 or so before you can remove roles from user. 

Replace

  if (previous.u_buying_assistant != '') { //Code to remove the role from previous owner.
                    var removeRole = new GlideRecord('sys_user_has_role');
                    removeRole.addQuery('user', previous.u_buying_assistant);
                    removeRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    //remove.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRole.next()) {
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRole.deleteRecord();
                    }
                }

with

 

if (previous.u_buying_assistant != '') { //Code to remove the role from previous owner.
                    var removeRole = new GlideRecord('sys_user_has_role');
                    removeRole.addQuery('user', previous.u_buying_assistant);
                    removeRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    removeRole.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRole.next()) {
removeRole.inherited=false;
removeRole.update();
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRole.deleteRecord();
                    }

  var removeRoles = new GlideRecord('sys_user_has_role');
                    removeRoles.addQuery('user', previous.u_buying_assistant);
                    removeRoles.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    removeRoles.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRoles.next()) {
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRoles.deleteRecord();
                    }
                }

 

Sandeep Rajput
Tera Patron
Tera Patron

@Rosy14 Please update your script as follows and it will work on remove too.

 

(function executeRule(current, previous /*null when async*/ ) {
    if (current.u_buying_assistant != previous.u_buying_assistant) {
        var userRole = new GlideAggregate('sys_user_has_role');
        userRole.addQuery('user', current.u_buying_assistant); //replace with your 
        userRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41'); //Add your role sys_id here
        userRole.addAggregate('COUNT');
        userRole.query();
        if (userRole.next()) {
            var roleCount = 0;
            roleCount = userRole.getAggregate('COUNT');
            gs.addInfoMessage(roleCount);
            if (roleCount == 0) { //Only add role if the user doesn't have it
                var glideRole = new GlideRecord('sys_user_has_role');
                glideRole.initialize();
                glideRole.user = current.u_buying_assistant;
                glideRole.role = '2def524a1bfffd100f52a797b04bcb41';
                glideRole.state = 'active';
                glideRole.insert();
			}
                if (previous.u_buying_assistant != '') { //Code to remove the role from previous owner.
                    var removeRole = new GlideRecord('sys_user_has_role');
                    removeRole.addQuery('user', previous.u_buying_assistant);
                    removeRole.addQuery('role', '2def524a1bfffd100f52a797b04bcb41');
                    removeRole.query();
                    gs.addInfoMessage('Previous owner ' + previous.u_buying_assistant);
                    if (removeRole.next()) {
                        gs.addInfoMessage('Deleting the record ' + removeRole.getValue('sys_id'));
                        removeRole.deleteRecord();
                    }
                }

            }
        }
		
})(current, previous);

Hope this helps.

Rosy14
Tera Guru

Thanks @Sandeep Rajput . Working now.

@Sandeep Rajput 

here I added one small script 

if (current.u_buying_assistant != '') {
                    glideRole.insert();
                }
becuase when the current field is emply then removing is fine but it again creating a new record with empty user and role.