The CreatorCon Call for Content is officially open! Get started here.

Remove Roles From a Group

Surbhi16
Tera Contributor

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

17 REPLIES 17

Nikhil65
Mega Guru

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.

Hi Nikhil,

Thanks for your response, but its not working out for me, I am sharing the screenshot of what I achieve after using your above script;

find_real_file.png

 

find_real_file.png

Regards,

Surabhi

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.