script include: build up the results as a JSON object (array)

 

function call

var res= findReferences ('963abacbdb2160101beb298a4896xxxx' , 'incident');

gs.print(JSON.stringify(res));

output

*** Script:

[{"display_value":"INC095xxxx",

"table":"incident",

"table_label":"Incident",

"sys_id":"963abacbdb2160101beb298a4896xxxx"},

{"display_value":"IA000xxxx","table":"incident_alert","table_label":"Incident Communication Plan","sys_id":"22ba953c37782f0088a85aa54399xxxx"},

{"display_value":"IA000xxx1","table":"incident_alert","table_label":"Incident Communication Plan","sys_id":"7e91b3a637912f0488a85aa54399xxxx"},

{"display_value":"IA000xxx2","table":"incident_alert","table_label":"Incident Communication Plan","sys_id":"946b378adbd66b00d2467195ae96xxxx"},

{"display_value":"SCTASK003xxxx","table":"sc_task","table_label":"Catalog Task","sys_id":"dae2586ddb836f041b4ffc45ae96xxxx"},

{"display_value":"SCTASK003xxxx","table":"sc_task","table_label":"Catalog Task","sys_id":"30369475db0baf041b4ffc45ae96xxxx"},

{"display_value":"SCTASK050xxxx","table":"sc_task","table_label":"Catalog Task","sys_id":"cda660601b1e2410e6dddce3b24bxxxx"}]

function:

findReferences = function(sReferenceId, sReferenceTable) {

var aFoundRecords = []; var grDictionary = new GlideRecord('sys_dictionary'); grDictionary.addEncodedQuery('internal_type=reference^reference=' + sReferenceTable + '^active=true^name!=null'); grDictionary.orderBy('name'); grDictionary.query(); while (grDictionary.next()) { var sTableName = grDictionary.getValue('name'); var sFieldName = grDictionary.getValue('element'); // Ignore tables that contain var_ as well as other qualified tables (reducing load) if (sTableName.match('^var_') || sTableName.match('^ecc_') || sTableName.match('^ts_') || sTableName.match('^ha_') || sTableName.match('^syslog') || sTableName.match('^clone')) { continue; } // No search list of tables if (sTableName == 'syslog' || sTableName == 'sys_audit' || sTableName == 'sys_watermark' || sTableName == 'text_search' || sTableName == 'new_call' || sTableName == 'sys_history' || sTableName == 'u_m2m_groups_users') { continue; } // Check each table.field and push findings into an array var grGenericTable = new GlideRecord(sTableName); grGenericTable.addActiveQuery(); grGenericTable.addQuery(sFieldName, sReferenceId); grGenericTable.query(); while (grGenericTable._next()) { aFoundRecords.push({ "display_value" : grGenericTable.getDisplayValue(), "table" : grGenericTable.getRecordClassName(), "table_label" : grGenericTable.getClassDisplayValue(), "sys_id" : grGenericTable.getUniqueValue() }); } } // return array to caller return aFoundRecords; };

Comments

Popular posts from this blog

GlideRecord setValue

Running transform maps asynchronously

variable advanced reference qualifier example