Last active
September 4, 2025 03:28
-
-
Save codaroma/d4ed229af70ca3f5f7b78f5ae2f0d24f to your computer and use it in GitHub Desktop.
Count reference to legacy SW models
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /*jshint esversion: 6 */ | |
| (function () { | |
| function getReferences(table) { | |
| var result = []; | |
| var gr = new GlideRecord("sys_dictionary"); | |
| var query = [ | |
| ["nameSTARTSWITHcmdb_ci", "element!=model_id"], | |
| [ | |
| "glidefunction:substring(name, 1, 7)!=cmdb_ci", | |
| "glidefunction:substring(name, 1, 5)!=var__", | |
| ], | |
| ] | |
| .map((e) => | |
| ["internal_type=reference", "reference=" + table] | |
| .concat(e) | |
| .join("^") | |
| ) | |
| .join("^NQ"); | |
| gr.addQuery(query); | |
| gr.orderBy("name"); | |
| gr.orderBy("element"); | |
| gr.query(); | |
| while (gr.next()) { | |
| result.push({ | |
| table: gr.name.toString(), | |
| element: gr.element.toString(), | |
| }); | |
| } | |
| return result; | |
| } | |
| function getRowCount(table, query) { | |
| var ga = new GlideAggregate(table); | |
| ga.addAggregate("COUNT"); | |
| ga.addQuery(query); | |
| ga.query(); | |
| if (!ga.next()) return 0; | |
| return Number(ga.getAggregate("COUNT")); | |
| } | |
| return [] | |
| .concat( | |
| getReferences("cmdb_model").map((ref) => | |
| Object.assign({}, ref, { | |
| count: getRowCount( | |
| ref.table, | |
| [ | |
| `${ref.element}ISNOTEMPTY`, | |
| `${ref.element}.sys_class_name=cmdb_software_product_model`, | |
| `${ref.element}.ref_cmdb_software_product_model.productISEMPTY`, | |
| `OR${ref.element}.ref_cmdb_software_product_model.x_snsab_snow_catal_snow_application_idISNOTEMPTY`, | |
| ].join("^") | |
| ), | |
| }) | |
| ), | |
| getReferences("cmdb_software_product_model").map((ref) => | |
| Object.assign({}, ref, { | |
| count: getRowCount( | |
| ref.table, | |
| [ | |
| `${ref.element}ISNOTEMPTY`, | |
| `${ref.element}.productISEMPTY`, | |
| `OR${ref.element}.x_snsab_snow_catal_snow_application_idISNOTEMPTY`, | |
| ].join("^") | |
| ), | |
| }) | |
| ) | |
| ) | |
| .filter((e) => e.count > 0) | |
| .sort( | |
| (a, b) => | |
| a.table.localeCompare(b.table) || | |
| a.element.localeCompare(b.element) | |
| ); | |
| })(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment