Get all references by sys_id from all tables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2018 05:45 AM
Hi,
I want to delete a record corresponding to a group. To achieve this without any issues by getting references to the record which should be deleted I need to know where a record is referenced. Is there any way to get a global list of tables which reference to a specific record?
Thanks,
Onur
- Labels:
-
Best Practices

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-15-2018 06:32 AM
There have been a number of ways to do this. One way is to run a background script to query all the tables values.... as described here;
https://snprotips.com/blog/sncprotips/2015/12/locate-any-record-in-any-table-by-sysidhtml?rq=search
function findAnywhere(sysid, html) {
if (html !== true && html !== 'true') { html = false; }
var check;
var tableName;
var url = gs.getProperty('glide.servlet.uri');
var table = new GlideRecord('sys_db_object');
//Make sure we're not looking at a ts (text search) table.
table.addEncodedQuery('sys_update_nameISNOTEMPTY^nameISNOTEMPTY^nameNOT LIKEts_');
table.query();
while (table.next()) {
tableName = table.getValue('name');
check = new GlideRecord(tableName);
if (check.get(sysid)) {
url += tableName + '.do?sys_id=' + sysid;
if (html) {
var htmlUrl = '<a href="' + url + '">' + url + '</a>';
return url;
}
else {
return url;
}
}
}
}
gs.print(findAnywhere('9ac3fd684fe1020066168ab18110c793', false));
I think this is great but if you find yourself doing this often consider installing this scoped app, the new version has sysid search too.
https://gitlab.com/jacebenson/servicenow-codesearch/tree/docs
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2022 07:56 AM
This example doesn't really address the OP's question which is "Is there any way to get a global list of tables which reference to a specific record?".
In other words...find a list of all records in any tables which have a reference to a specific record.
Instead, the example shown simply finds the record with a specific sys id.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-02-2022 06:01 PM
Hey Chris,
There may be better ways to do this. This script i provided would print out everything linking to the record. Its not a "...list of tables reference to a specific record" but, it is a list of records referencing the specific table.
If you have a newer solution and could share I would love that.
Thanks