Remove Roles From a Group
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-30-2022 04:08 AM
HI,
How can we remove role from a group using script?
Is there any way we can delete any role from a group based on user input?
Regards,
Surbhi
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2022 03:29 AM
Hi Chaitanya,
It is highly possible that you are passing name, and referencing sysid instead.
Use the below code - its tried and tested, if its not working, then it is sure that the values of group and role is not populating correctly.
var groupName = vaInputs.group_name;
var roleName = vaInputs.group_role_to_be_removed;
gs.info("Group name is : "+groupName+" and role to be deleted is : "+roleName);
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('group.name',groupName);
gr.addQuery('role.name','itil');
gr.query();
while(gr.next()){
gr.deleteRecord();
gs.info("deleted");
}
If this doesn't work by any chance, then please share the values of group and role that will get populated in logs.
Please mark my answer as correct and helpful, if it solves your issue.
Thanks,
Nikhil Nair.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-01-2022 03:42 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-04-2022 01:01 AM
Hi Surabhi,
I can see in your image that sysid is being passed, so above code will not work.
I have never worked with Virtual Agent. But anyways give a try with below code.
var groupName = vaInputs.group_name;
var roleName = vaInputs.group_role_to_be_removed;
var gr = new GlideRecord('sys_group_has_role');
gr.addQuery('group',groupName);
gr.addQuery('role',roleName);
gr.query();
while(gr.next()){
gr.deleteRecord();
gs.info("deleted");
}
If it still doesn't work then please refer the attached links, they should help.
https://community.servicenow.com/community?id=community_question&sys_id=15c86a1cdbcf90d066f1d9d968961941
https://community.servicenow.com/community?id=community_question&sys_id=53ca1da1db2cec10190b1ea668961962
If the code or links solve your issue, mark the answer as correct.
Thanks,
Nikhil.