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

URL link in addInfoMessage

var ga=new GlideAjax('gld_HR_ajax'); ga.addParam('sysparm_name', 'checkEmployeeNumber_hrProfile'); ga.addParam('sysparm_hrprofilenumber', g_form.getValue('number')); ga.addParam('sysparm_employeenumber', newValue); ga.getXMLAnswer(function(answer) { if (answer!='undefined' && answer!=''){ var navURL="<a style='text-decoration:underline;color:blue' href=hr_profile.do?sysparm_query=number=" + answer + ">" + answer + "</a><img width='3' src='images/s.gif'/>"; var sMsg='The employee number entered already exists on another HR Profile ' + navURL; //alert(sMsg); g_form.showErrorBox('employee_number', 'error - please check'); g_form.addInfoMessage(sMsg); } });

GlideRecord setValue

setValue(String name, Object value) Sets the specified field to the specified value. Normally a script would do a direct assignment, for example,  gr.category = value . However, if in a script the element name is a variable, then  gr.setValue(elementName, value)  can be used. When setting a value, ensure the data type of the field matches the data type of the value you enter. This method cannot be used on journal fields. If the value parameter is null, the record is not updated, and an error is not thrown https://developer.servicenow.com/app.do#!/api_doc?v=madrid&id=r_GlideRecord-setValue_String_Object