- 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 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:21 AM
Hi
I tried with this code but
deleteRecord() is not working.
Thank You
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-30-2022 09:49 AM
Can you check if the role assigned to that user is inherited from any other role or group?
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 12:44 AM
Hi,
Are you sure it's going inside the if condition?
what's the error thrown?
Is the job in HR scope?
If yes then check this
1) Did you check Can Delete is Checked for sys_user_has_role table
If not then any before delete BR on sys_user_has_role table is restricting the delete
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())
{
gs.info("Inside before deleting");
rec.deleteRecord();
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader