script add a role to a User

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2022 01:31 AM
5 REPLIES 5
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2022 01:34 AM
Hi Ha Le
You can add role to user manually, what is the requirement?
sample code:
Map role sys_id,user sys_id accordingly.
var grSysUserHasRole = new GlideRecord('sys_user_has_role');
grSysUserHasRole.initialize();
grSysUserHasRole.setValue('user' ,'user_sysid');
grSysUserHasRole.setValue('role','role_sysid');
grSysUserHasRole.insert();
Mark it helpful if this helps you to understand. Accept solution if this give you the answer you're looking for
Kind Regards,
Rohila V
2022-25 ServiceNow Community MVP

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2022 01:53 AM
i want to add role admin for user itil

Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2022 01:54 AM
seem like above not work
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
ā04-15-2022 01:38 AM
As a best practice you should add user to a group rather than giving the role directly .
var gr = new GlideRecord("sys_user_grmember"); // group member
gr.initialize();
gr.group ='Analysts';
gr.user='Paul Martin';
gr.insert();
Hope its helpful
Regards,
Anshu
Anshu