Comments on: Find all System References to a Specific Record https://servicenowguru.com/system-definition/find-references-specific-record/ ServiceNow Consulting Scripting Administration Development Fri, 20 Sep 2024 16:01:52 +0000 hourly 1 https://wordpress.org/?v=6.8.2 By: Randall King https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9547 Fri, 12 May 2023 22:44:27 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9547 This was very helpful!!!

]]>
By: Chris Bui https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9542 Sat, 29 Jul 2017 17:45:37 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9542 Hi Mark!

Just a heads up, Jakarta seems to handle line breaks in gs.addInfoMessage differently. I put a br tag at the end of the msg lines and it fixed the problem.

Also, we rely a lot on this functionality, especially when it comes to assignment groups. If someone asks to rename a group we use this to see what catalog items, assignment rules, scripts, etc. that will be affected. Right or wrong, there’s a lot of .setDisplayValue() going on here and it needs to be maintained.

To solve this, I created a function to check for named references if we’re checking a group. You can easily add/remove areas you need to check for named references. It’s pretty handy so I wanted to share. Added the following code before your gs.addInfoMessage(msg) at the end.

//Query for name references if it’s a group
if(current.getTableName() == ‘sys_user_group’){
var nameMsg = ‘Matching SCRIPT fields (Assignment Rules, Record Producers, Client Scripts(client, catalog, wizard), and Workflows) where this record is referenced by NAME are displayed below…‘;

nameMsg += findTableReferences(‘sysrule_assignment’, ‘script’, current.name);
nameMsg += findTableReferences(‘sc_cat_item_producer’, ‘script’, current.name);
nameMsg += findTableReferences(‘sys_script_client’, ‘script’, current.name);
nameMsg += findTableReferences(‘catalog_script_client’, ‘script’, current.name);
nameMsg += findTableReferences(‘expert_script_client’, ‘script’, current.name);
nameMsg += findTableReferences(‘sys_script_include’, ‘script’, current.name);
nameMsg += findTableReferences(‘u_workflow_script’, ‘val_value’, current.name);
gs.addErrorMessage(nameMsg);
}

And here’s the function…

function findTableReferences(tblName, column, name){
var message = ”;
var recMsg = ‘ records found’;
var workflowURLQuery = ”;
var prefix = ‘Table’;

var gr = new GlideRecord(tblName);
gr.addQuery(column, ‘CONTAINS’, name);

//for the client script table, only return client scripts and not catalog or wizard
if(tblName == ‘sys_script_client’)
gr.addQuery(‘sys_class_name’, ‘sys_script_client’);

if(tblName == ‘u_workflow_script’){
gr.addQuery(‘wfa_workflow_version.published’, true);
workflowURLQuery = ‘wfa_workflow_version.published=true^’;
prefix = ‘Workflow Activity’;
}
gr.query();

if(gr.getRowCount() == 1){
recMsg = ‘ record found’;
}

if(gr.getRowCount() > 0){
message = ‘–‘ + prefix + ‘: ‘ + tblName + ‘‘ + ‘ – Column [Column type]: ‘ + column + ‘ [‘ + ‘string’ + ‘]’ + ‘ — ‘ + ” + ‘‘ + gr.getRowCount() + recMsg + ‘‘ + ‘.’ + ”;
}else{
message = ‘–‘ + prefix + ‘: ‘ + tblName + ‘‘ + ‘ – Column: ‘ + column + ‘‘ + ‘ — ‘ + ‘NOTHING FOUND’;
}

return message;
}

]]>
By: Barry Jones https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9541 Wed, 17 May 2017 21:49:20 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9541 Super helpful.
Nit pick, but add semi-colon to:
Condition: gs.hasRole(‘admin’)
and
Client: true

Great post.

]]>
By: Mark Stanger https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9540 Wed, 19 Apr 2017 13:43:27 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9540 In reply to Christian Probst.

Thanks Christian, I haven’t heard of that happening before but I’ll keep an eye on it.

]]>
By: Christian Probst https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9539 Wed, 19 Apr 2017 13:29:59 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9539 Hi,
when running against an Incident record we got an error message “Rule entry under cmdb_ci_endpoint_storf identifier using non-existent field is ignored during identification!”. I don;t how that ci class is relevant for the incident so no issue at this point, just want to let you guys know :-)
Cheers, Christian

]]>
By: Mark Stanger https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9538 Tue, 21 Mar 2017 10:26:10 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9538 In reply to Peteris B.

Thanks for the heads up! I haven’t seen this before so I’m not sure it applies everywhere but this will be good to have here just in case someone else encounters the issue.

]]>
By: Peteris B https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9537 Tue, 21 Mar 2017 08:17:16 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9537 Not sure if this is related only to Helsinki release, but the “audit and log fields” exclusion list, in my case, had to be populated with “dl_matcher” and “grc_risk”.

dict.addQuery(‘name’, ‘DOES NOT CONTAIN’, ‘dl_matcher’);
dict.addQuery(‘name’, ‘DOES NOT CONTAIN’, ‘grc_risk’);

This is in case for the find record reference script to work when attempting to execute the action on Users table.

I believe its due to the base tables not being accessible for the admin user. The missing table error is also posted to ServiceNow logs.

]]>
By: Mark Stanger https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9536 Mon, 29 Feb 2016 16:27:48 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9536 In reply to Markus Schär.

Awesome, thanks for the update! I’ve adjusted the solution above with this fix.

]]>
By: Markus Schär https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9535 Mon, 29 Feb 2016 14:55:55 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9535 This is an excellent script and we are still using it frequently.

In Fuji, one thing I noticed though is that addEncodedQuery in combination with addOrCondition is not working as expected anymore. The ^OR conditions seem to be ignored. One way to fix this issue is to modify line 24 (replace addEncodedQuery by addQuery):

dict.addQuery(‘reference’, ‘IN’, refTable).addOrCondition(‘internal_type’, ‘document_id’).addOrCondition(‘internal_type’, ‘conditions’);

// Test with Fuji Patch 10
var dict = new GlideRecord(‘sys_dictionary’);
dict.addEncodedQuery(‘referenceIN’ + ‘core_company’).addOrCondition(‘internal_type’, ‘document_id’).addOrCondition(‘internal_type’, ‘conditions’);
gs.log(dict.getEncodedQuery()); // *** Script: referenceINcore_company

var dict = new GlideRecord(‘sys_dictionary’);
dict.addQuery(‘reference’, ‘IN’, ‘core_company’).addOrCondition(‘internal_type’, ‘document_id’).addOrCondition(‘internal_type’, ‘conditions’);
gs.log(dict.getEncodedQuery()); // *** Script: referenceINcore_company^ORinternal_type=document_id^ORinternal_type=conditions

]]>
By: James Fricker https://servicenowguru.com/system-definition/find-references-specific-record/#comment-9534 Mon, 30 Nov 2015 01:35:00 +0000 https://servicenowguru.wpengine.com/?p=4957#comment-9534 In reply to James Fricker.

Obviously adding the line rec.setWorkflow(false); would help avoid query business rules.

]]>