- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 12:35 AM
I have written this below code in schedule Job, but not able to remove the role from User's profile
---------------------------------------------------------------------------------------------
var userRec = new GlideRecord('sn_hr_core_profile');
userRec.addEncodedQuery('user.active=true^employment_start_dateRELATIVELT@dayofweek@ago@3');
userRec.query();
while(userRec.next())
{
var rec = new GlideRecord('sys_user_has_role');
rec.addQuery('user',userRec.user.sys_id);
rec.addQuery('role','a37b3f710b03120025666f3ef6673abf');
rec.setWorkflow(false);
rec.query();
if(rec.next())
{
rec.deleteRecord();
}
----------------------------------
Kindly please help
Thank you
Solved! Go to Solution.
- Labels:
-
Team Development
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 12:42 AM
Hi Rubina
Try this code
var userRec = new GlideRecord('sn_hr_core_profile');
userRec.addEncodedQuery('user.active=true^employment_start_dateRELATIVELT@dayofweek@ago@3');
userRec.query();
while (userRec.next()) {
var rec = new GlideRecord('sys_user_has_role');
rec.addQuery('user', userRec.user.sys_id);
rec.addQuery('role', 'a37b3f710b03120025666f3ef6673abf');
rec.query();
if (rec.next()) {
rec.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
‎03-30-2022 09:17 AM
Hi
Yes it is going inside loop
Yes job in HR scope
Yes checked Can Delete is Checked for sys_user_has_role table
not getting any error but deleteRecord() is not working
Thank you
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 08:44 PM
Hi,
It seems you forgot to check this
any before delete BR on sys_user_has_role table is restricting the delete
Did you check any cross scope access issue?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 08:51 PM
Hi,
go to table sys_user_has_role and check all checkbox and click update, it's work for me: