- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:01 AM
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);
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:15 AM
@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.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:15 AM
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();
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:15 AM
@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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:27 AM
Thanks @Sandeep Rajput . Working now.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-25-2024 12:37 AM
here I added one small script