- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 10:19 PM
Hi,
I have a requirement where we want to add/remove a particular role from user profile when a Checkbox "xyzRole" available in the custom application form is checked/ unchecked.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 12:35 AM - edited 09-15-2023 12:38 AM
Hello @Puneet4418 ,
Try Insert/Update - After Business rule on a User table, You said, you have a custom check box for xyzRole. So try below code (you can modify code according to your requirement)
(function executeRule(current, previous /*null when async*/) {
var roleValue = current.u_demo_test_role; // back end name of custom check box of xyzRole
// gs.addInfoMessage(roleValue);
if(roleValue == true){
var addRole = new GlideRecord('sys_user_has_role');
addRole.initialize();
addRole.user = current.sys_id;
addRole.role = '1fff282147dd71109f1d374a436d432b'; // sys_id of role, which is given in check box
addRole.insert();
// gs.addInfoMessage('Role inserted');
}
else if(roleValue == false){
var removeRole = new GlideRecord('sys_user_has_role');
removeRole.addEncodedQuery("user="+current.sys_id+"^role=1fff282147dd71109f1d374a436d432b"); // sys_id of role, which is given in check box
removeRole.query();
if(removeRole.next()){
removeRole.deleteRecord();
}
// gs.addInfoMessage('Role Removed');
}
})(current, previous);
Please mark my answer as helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 10:21 PM
Hi,
You can write a business rule to achieve the same.
Thanks and Regards,
Saurabh Gupta
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-14-2023 10:32 PM
you can use business rule or flow designer for this
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 12:35 AM - edited 09-15-2023 12:38 AM
Hello @Puneet4418 ,
Try Insert/Update - After Business rule on a User table, You said, you have a custom check box for xyzRole. So try below code (you can modify code according to your requirement)
(function executeRule(current, previous /*null when async*/) {
var roleValue = current.u_demo_test_role; // back end name of custom check box of xyzRole
// gs.addInfoMessage(roleValue);
if(roleValue == true){
var addRole = new GlideRecord('sys_user_has_role');
addRole.initialize();
addRole.user = current.sys_id;
addRole.role = '1fff282147dd71109f1d374a436d432b'; // sys_id of role, which is given in check box
addRole.insert();
// gs.addInfoMessage('Role inserted');
}
else if(roleValue == false){
var removeRole = new GlideRecord('sys_user_has_role');
removeRole.addEncodedQuery("user="+current.sys_id+"^role=1fff282147dd71109f1d374a436d432b"); // sys_id of role, which is given in check box
removeRole.query();
if(removeRole.next()){
removeRole.deleteRecord();
}
// gs.addInfoMessage('Role Removed');
}
})(current, previous);
Please mark my answer as helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-15-2023 01:20 AM
The simplest and way would be to create a flow designer on trigger of that field change, which in turn adds the Role to the user.
Thank you,
Vedang