How we can remove Inactive users from assignment lookup table from three instances using SJ

Amrutha K V
Tera Contributor

We have a requirement for removing the inactive user from assignment lookup table from three instances.
1)https://ab.service-now.com/ 2)https://abc.service-now.com/ 3)https://abcd.service-now.com/
I tried this script and it is removing user from assignment lookup table.
var user = new GlideRecord('sys_user');
user.addQuery('active', false);
user.query();
while (user.next()) {
var rec = new GlideRecord('dl_u_assignment');
rec.addEncodedQuery('assigned_toISNOTEMPTY');
rec.query();
while (rec.next()) {
if (user.sys_id == rec.assigned_to.sys_id) {
rec.assigned_to = '';
rec.update();
}
}
But not sure how we can fetch the three different instance name and based on the instance name how we can set the logic within the same script via scheduled job.
If possible can you please add that logic in the above script.

2 REPLIES 2

Aman Kumar S
Kilo Patron

Hi @Amrutha K V ,

This job is generic, you can put it down in the fix script and move the script in the respective instances, and run the fix script.

Otherwise you will need to run API and that is going to be tedious process.

 

Best Regards
Aman Kumar

Tushar
Kilo Sage
Kilo Sage

Hi @Amrutha K V 

 

Can we implement something like this?

var instanceURLs = [
  'https://ab.service-now.com/',
  'https://abc.service-now.com/',
  'https://abcd.service-now.com/'
];

for (var i = 0; i < instanceURLs.length; i++) {
  var instanceURL = instanceURLs[i];
  var user = new GlideRecord('sys_user');
  user.addQuery('active', false);
  user.query();
  
  while (user.next()) {
    var instance_name = user.getFieldValue('sys_domain');
    
    if (instance_name === instanceURL) {
      var rec = new GlideRecord('dl_u_assignment');
      rec.addEncodedQuery('assigned_toISNOTEMPTY');
      rec.query();
      
      while (rec.next()) {
        if (user.sys_id == rec.assigned_to.sys_id) {
          rec.assigned_to = '';
          rec.update();
        }
      }
    }
  }
}

 


Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar