Adding a role to user when added to a custom field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2023 11:43 AM
Hello All,
I have a requirement on the knowledge module, where on the knowledge category I have added a custom field, and when a user is added to the field it gives them the knowledge_manager role. Does anyone have a script to do this or know of the best way? Issue i'm getting in my script is that when one user is added it gives them the role, when I add another user after that it gives a unique key violation and removes the role from the user that is still in the field.
Many Thanks,
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2023 12:23 PM
Hi Danish,
Thanks so much for supplying me with this. So i've used this script and it adds the knowledge_manager role as I need it to. The bit I've been struggling with is that I also need it to remove the role, when the user is removed from the custom field, without removing the role for the others still in the field?
Many Thanks,
Luke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2023 08:00 PM
Hi @Luke James ,
Plz try below logic for removal.
(function executeRule(current, previous /*, g*/) {
var userList = current.custom_field; // Replace with the actual field name
// Check if the custom field is modified
if (current.custom_field != previous.custom_field) {
// Remove the role from users who are no longer in the custom field
var removedUsers = previous.custom_field.split(',').filter(function(user) {
return userList.indexOf(user.trim()) === -1;
});
for (var i = 0; i < removedUsers.length; i++) {
var removedUser = removedUsers[i].trim();
// Check if the user exists and remove the role
var userGr = new GlideRecord('sys_user_has_role');
userGr.addQuery('user', user);
userGr.addQuery('role','knowledge_manager');
if(userGr.next()){
userGr.deleteRecord();
}
}
}
})(current, previous);
Thanks,
Danish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-26-2023 07:32 PM
HI @Luke James ,
I trust you are doing great.
Please find the below code for the same :
(function executeRule(current, previous /*null when async*/) {
var usersField = current.getValue('your_custom_field'); // Replace 'your_custom_field' with the actual field name
var usersArray = usersField.split(','); // Assuming the users are stored in a comma-separated format
usersArray.forEach(function(userId) {
var userGr = new GlideRecord('sys_user');
if (userGr.get(userId.trim())) {
var roleGr = new GlideRecord('sys_user_has_role');
roleGr.addQuery('user', userId.trim());
roleGr.addQuery('role', 'knowledge_manager'); // Replace with the sys_id of the knowledge_manager role
roleGr.query();
if (!roleGr.next()) {
// User does not have the role, so assign it
roleGr.initialize();
roleGr.user = userId.trim();
roleGr.role = 'knowledge_manager'; // Replace with the sys_id of the knowledge_manager role
roleGr.insert();
}
// Else, the user already has the role, do nothing
}
});
// Additional logic to remove the role from users no longer in the field, if necessary
})(current, previous);
Was this answer helpful?
Please consider marking it correct or helpful.
Your feedback helps us improve!
Thank you!
Regards,
Amit Gujrathi