- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 12:43 AM
Hello Experts,
I have a requirement where there is a field Position on the user table and its choice field and choices are 'Non-Agent' and 'Agent' if 'Non-Agent' then I have to remove the role.
I am trying by background script but not getting the expected result
Please help me with this.
var user = new GlideRecord('sys_user');
user.addQuery('u_position','non_agent');
user.query();
while (user.next()) {
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user', user.sys_id);
hasRole.addQuery('user.role', 'itil');
hasRole.query();
if (hasRole.next()) {
hasRole.deleteRecord;
}
}
Thanks in advance, atik.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 12:51 AM
Hi @Atik
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 12:55 AM
You can also try this way without querying the user table.
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user.u_position', 'non_agent');
hasRole.addQuery('user.role', 'itil');
hasRole.query();
if (hasRole.next()) {
hasRole.deleteRecord();
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 12:51 AM
Hi @Atik
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 12:55 AM
You can also try this way without querying the user table.
var hasRole = new GlideRecord('sys_user_has_role');
hasRole.addQuery('user.u_position', 'non_agent');
hasRole.addQuery('user.role', 'itil');
hasRole.query();
if (hasRole.next()) {
hasRole.deleteRecord();
}
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-17-2024 02:16 AM
Hey, @Voona Rohila thanks it worked