jacebenson / jace.pro

A blog about servicenow and other technology
https://jace.pro
20 stars 17 forks source link

How to get the current shard for a table #271

Open jacebenson opened 3 years ago

jacebenson commented 3 years ago
// The following script (or variant thereof) can be used in 
// /sys.scripts.do or in a server-side script to determine
// the current shard for a table. 

printCurrentShard('sysevent'); 

function printCurrentShard(pTableName) { 
  var gr = new GlideRecord('sys_table_rotation'); 
  if (pTableName) { 
    gr.addQuery('name', '=', pTableName); 
  } 
  gr.query(); 
  while (gr.next()) { 
    try { 
      var tre =
Packages.com.snc.db.replicate.TableRotationExtensions.get();

      var extension = tre.getExtension(gr.name); 
      var tablename = extension.getTableName(); 
    } catch (e) { 
      gs.print('Error getting current shard for table ' + gr.name); 
    } finally { 
      gs.print('The current shard of table ' + gr.name + ' is ' + tablename); 
    } 
  } 
}