Get all references by sys_id from all tables

onurg_l
Kilo Contributor

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

3 REPLIES 3

Jace Benson
Mega Sage

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

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.

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