script to show all tables associated with the user on sys_user table

nebiata
Tera Expert

I have a requirement to write a  script to show all tables associated with the user on sys_user table. 

3 REPLIES 3

Sonu Parab
Mega Sage
Mega Sage

Hello @nebiata ,

 

To retrieve all tables associated with a user on the sys_user table in ServiceNow, you can use the GlideRecord API in a script. Here's an example script that demonstrates how to achieve this:

 

// Create a GlideRecord object for the sys_user table
var userGR = new GlideRecord('sys_user');

// Add a query condition to filter by the user's username or any other relevant field
userGR.addQuery('user_name', gs.getUser().getUserName());

// Query the sys_user table
userGR.query();

// Iterate through the results and display the associated tables
while (userGR.next()) {
// Get the associated tables for the user
var userTables = userGR.getAssociatedTables();

gs.info('Tables associated with user ' + userGR.user_name + ':');

// Display each associated table
for (var i = 0; i < userTables.size(); i++) {
var tableName = userTables.get(i).toString();
gs.info(tableName);
}
}

 

 

This script does the following:

  1. Creates a GlideRecord object for the sys_user table.
  2. Adds a query condition to filter by the user's username (modify as needed).
  3. Queries the sys_user table.
  4. Iterates through the results and retrieves the associated tables for each user.
  5. Displays the associated tables using gs.info().

Make sure to run this script in the appropriate context, such as a Business Rule or Script Include, depending on your requirements.

 

Please mark this response as correct and/or helpful if it assisted you

Thank you.

nebiata
Tera Expert

Thank you Sonu. I try that script and I got this script.png

 

The plan is that to put a link with rl.pngthis script under a related link using UI action so when someone clicks on the link, will shoe all tables related to that user.

nebiata
Tera Expert

Hello @Sonu  

Now when I run the script, it returns all the users. Any suggestion?

 

Thank you