Skip to main content

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

ServiceNow check for null or nil or empty (or not)

Haven't tested these all recently within global/local scopes, so feel free to have a play! option 1 use an encoded query embedded in the GlideRecord , e.g.  var grProf = new GlideRecord ( 'x_cls_clear_skye_i_profile' ); grProf . addQuery ( 'status=1^ owner=NULL ' ); grProf . query (); even better use the glideRecord  addNotNullQuery or addNullQuery option 2 JSUtil.nil / notNil (this might be the most powerful. See this link ) example: if ( current . operation () == 'insert' && JSUtil . notNil ( current . parent ) && ! current . work_effort . nil ())  option 3 there might be times when you need to get inside the GlideRecord and perform the check there, for example if the code goes down 2 optional routes depending on null / not null can use gs.nil : var grAppr = new GlideRecord ( 'sysapproval_approver' ); var grUser = new GlideRecord ( 'sys_user' ); if ( grUser . get ( 'sys_id' , current . approver )){

Get URL Parameter - server side script (portal or classic UI)

Classic UI : var sURL_editparam = gs . action . getGlideURI (). getMap (). get ( ' sysparm_aparameter ' ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); } Portal : var sURL_editparam = $sp . getParameter ( " sysparm_aparameter " ); if ( sURL_editparam == 'true' ) { gs . addInfoMessage ( 'parameter passed ); }