logo

NJP

Script - Get table based on sysId

Import · Jan 26, 2021 · article

//Find table based on sys_id.This script should be run as admin user in global scope. Script gives output as table name and sysId

Link to video:https://youtu.be/\_DT34P734NA

github link : https://github.com/anilvaranasi/ServiceNow

//Developed by Srinivas Anil Kumar Varanasi//This script should be run as admin user in global scope//Script gives output as table name and sysId//Find table based on sys_id.runQueryonTables();function runQueryonTables() { var sLog = ''; var outputJson = {}; var tableList = []; var outputList = []; var sysId = '2f738cab0f3600105bc5e7b1d4767e3b'; // sys_id to be updated based on requirement var grTable = new GlideRecord('sys_db_object'); grTable.query(); while (grTable.next()) { tableList.push(grTable.name + ''); } for (var m = 0; m < tableList.length; m++) { outputJson = runQuery(tableList[m], sysId); outputList.push(outputJson); } for (var k = 0; k < outputList.length; k++) { if(outputList[k].output.length >0) { sLog += outputList[k].tableName + "=" + outputList[k].output + "\n"; } } gs.print("Tables matching sys_id =" + sLog);}function runQuery(tableName, sysId) { var output = ''; var outputJson = {}; try { var grQuery = new GlideRecord(tableName); grQuery.query('sys_id', sysId); grQuery.query(); if (grQuery.next()) { output = grQuery.sys_id + ''; } } catch (err) { output = 'no access to table ' + err; } outputJson = { "tableName": tableName, "output": output }; return (outputJson);

}

View original source

https://www.servicenow.com/community/now-platform-articles/script-get-table-based-on-sysid/ta-p/2319330