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

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

Hi @Rohila Voona,

I tried with this code but 

deleteRecord() is not working.

 

 

Thank You

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

Ankur Bawiskar
Tera Patron
Tera Patron

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

find_real_file.png

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

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