script to show all tables associated with the user on sys_user table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2023 07:48 AM
I have a requirement to write a script to show all tables associated with the user on sys_user table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2023 11:00 AM
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:
- Creates a GlideRecord object for the sys_user table.
- Adds a query condition to filter by the user's username (modify as needed).
- Queries the sys_user table.
- Iterates through the results and retrieves the associated tables for each user.
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-24-2023 11:23 AM
Thank you Sonu. I try that script and I got this
The plan is that to put a link with this script under a related link using UI action so when someone clicks on the link, will shoe all tables related to that user.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2023 10:08 AM