Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How do we find out the record, given the sys_id?

upasanamahanta
Mega Contributor

Given a sys_id, how do we find to which record the sys_id belongs to?

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

You COULD write a script that goes through sys_db_object and checks each table to see if it can find the record. With over 2,000 tables in a default Helsinki instance, this could take a while. The *untested* script might look something like this:



var mySysId = 'xxxxxxxxxxxxxxxxxxxxx'; // put in your sys_id here


var table = new GlideRecord('sys_db_object');


table.query();



while (table.next()) {


      var gr = new GlideRecord(table.getValue('name'));


      if (gr.get(mySysId)) {


                  gs.log('it looks like it came from ' + gr.getValue('label'));


                  break;


      }


}


View solution in original post

17 REPLIES 17

Not sure if I have done right but I always received below error whenever I ran it using “Scripts – Background” trying to find a record given its sys_id

 

*** Script: it looks like it came from null

 

var mySysId = '61aa0e5567012300b270c3105685ef19'; // put in your sys_id here

 

var table = new GlideRecord('sys_db_object');

 

table.query();

 

while (table.next()) {

       var gr = new GlideRecord(table.getValue('name'));

       if (gr.get(mySysId)) {

                   gs.log('it looks like it came from ' + gr.getValue('label'));

                   break;

       }

}

SaschaWildgrube
ServiceNow Employee
ServiceNow Employee

From time to time the solution needs a face-lift.

New tables appear in the platform and additional exceptions should be added.

The DevTools app contains powerful tools and a code library for ServiceNow developers.

One of them is the GetTableFromSysId() function. Check it out here:

https://github.com/saschawildgrube/servicenow-devtools/blob/master/update/sys_script_include_fc26194...

It excludes a number of tables from the search and searches a number of tables first, where I believe the likelihood of a match is higher - which makes it faster in many cases than many other proposed solutions.

mike555
Tera Expert

 

var mySysId = 'zzzzzz'; // put in your sys_id here

var table = new GlideRecord('sys_db_object');

table.query();

while (table.next()) {

    if (table.getValue('name')) {
        gs.debug("Table:" + table.getValue('name'));
        var label = table.getValue('label');

        if ((label != "label") && (!label.startsWith("Text index")) && (table.getValue('name'))) {
            var target = new GlideRecord(table.getValue('name'));
            try {
                target.addQuery("sys_id", mySysId);
                target.query();

                while (target.next()) {
                    gs.debug('It looks like it came from table: ' + target.getValue('name'));
                }
            } catch (e) {}
        }
    }
}

 

I really recommend to look into THIS solution here:

https://github.com/saschawildgrube/servicenow-devtools/blob/master/update/sys_script_include_fc26194...

1. It is faster

2. It ignores irrelevant tables

3. It looks into a set of tables first because it is more likely that a user searches for a sys_id from these tables (based on experience with a huge developer community and their feedback) - like "task" or "sys_metadata"

4. It ignores all tables that are extended from another table

5. It works across scope boundaries

😉

When I tried it as a Fix Script, it threw an exception, below.  

Evaluator: com.glide.script.RhinoEcmaError: "IsValidSysId" is not defined.
script : Line(12) column(0)
9: if (strSysId == 'global') {
10: return 'sys_dictionary';
11: }
==> 12: if (IsValidSysId(strSysId) == false) {

 

Commenting out the part led to another exception,

 

FastLock Swapped DBI since we were opening a second lock (RollbackContextState:457c6c191bc47114309bfb25464bcb72) on same connection
Completed: Service Mapping Recomputation 1.3dfce1201b132110309bfb25464bcb51 in 0:00:00.004, next occurrence is 14-07-2023 09:43:14, sys_created_on: 2023-06-04 22:42:28, trigger_type: 1 (Repeat), priority: 150, run_time: , repeat: 1970-01-01 00:00:05, previous next_action time: 2023-07-14 08:43:09.000 UTC, current next_action time: 2023-07-14 08:43:14.000 UTC, max_drift: null, queued on: 2023-07-14 08:43:09.062 UTC, execution time millis: 4, actual start time: 2023-07-14 08:43:09.102 UTC, actual end time: 2023-07-14 08:43:09.106 UTC
Evaluator: com.glide.script.RhinoEcmaError: "GetRecord" is not defined.
script : Line(32) column(0)
29: ];
30:
31: for (var nTable = 0; nTable < aPriorityTables.length; nTable++) {
==> 32: var grRecord = GetRecord(aPriorityTables[nTable], strSysId, true);
33: if (grRecord != false) {
34: return GetStringValue(grRecord.getRecordClassName());
35: }

[0:00:00.027] Total Time