The CreatorCon Call for Content is officially open! Get started here.

Delete orphan records from sys_user_has_role table

gee
Tera Guru

Hi Team,

I have an issue with deleting orphan records from sys_user_has_role table.

Tried with fix script to delete these records using GlideRecord, failed.

Delete button is disabled, even though schema level deletion is enable.

Also dried to deleted the user and imported the same user record using XML (user record export). Still role definitions are there!

Is there any method to delete these records from the above table?

Any suggestion please?

Platform is on Helsinki Patch 8.

Thanks & Regards,

Gee Abraham

1 ACCEPTED SOLUTION

gee
Tera Guru

Hi All,


This issue has been resolved by installing the following plugin.


Contextual Security: Role Management Enhancements (com.glide.role_management.inh_count)



Many thanks for your efforts to resolve this issue.



Best Regards,


Gee


View solution in original post

29 REPLIES 29

Hi Pradeep,



Does 'deleteMultiple();' work inside the while loop?


Hi Gee,



Can you removing update() and try it. Because deleteRecord will delete the record itself, so there is no option to update it and this is just my assumption.



var usr = new GlideAggregate("sys_user_has_role");


usr.addQuery("user", "7225d7164f03a2007b3be6518110c792");


usr.query();


while(usr.next())


  {


  usr.deleteRecord();


// usr.update();


  gs.log('********* sys_user_has_role ' + usr.role.name + ' ' + usr.granted_by);


  }


gee
Tera Guru

Hi Pradeep,


I have tried the given   commands and it did NOT worked. There may not be any workflows attached to this table's record.



Any other option please?


Thanks & Regards,


Gee


gee
Tera Guru

------


var usr = new GlideAggregate("sys_user_has_role");


usr.addQuery("user", "7225d7164f03a2007b3be6518110c792");


usr.query();


while(usr.next())


      {


              usr.setWorkflow(false);


              usr.deleteMultiple();


              gs.log('********* sys_user_has_role ' + usr.role.name + ' ' + usr.granted_by);


      }


------


Rajshekhar Pau1
Kilo Guru

Hi Gee,



Thanks for your concern regrading this,



Please use the below script in background script to delete the records from the table:



var users = new GlideRecord('sys_user');  


users.addQuery('user_name', 'bernard.laboy');  


users.query();  



if(users.next()){  


      var hasRole = new GlideRecord('sys_user_has_role');  


      hasRole.addQuery('user', users.sys_id);


      hasRole.query();  



while(hasRole.next()) {


gs.print(hasRole.getRowCount());


gs.print('********* sys_user_has_role ' + hasRole.role.name);


hasRole.deleteMultiple();  


      }  


}  




I have a given a try in my personal dev instance and it worked fine for me. Please modify the script if necessary.



Kindly let me know if that has worked and hit like or helpful based on impact.




Kind Regards,


Rajshekhar Paul