deleteRecord() is not working

Rubina Khatun1
Kilo Contributor

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 

1 ACCEPTED SOLUTION

Voona Rohila
Kilo Patron
Kilo Patron

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

View solution in original post

7 REPLIES 7

Hi @Ankur Bawiskar,

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

 

 

 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ha Le
Tera Expert

Hi, 

go to table sys_user_has_role and check all checkbox and click update, it's work for me: