Identify unused/empty custom attributes in a table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hi Community,
Is there any way to identify all unused/empty custom attributes in table? We have 200+ custom attributes created on base cmdb_ci table. We need to remove those unused/empty custom attributes. Checking 1 by 1 attribute is not possible.
Thanks in Advance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
https://www.servicenow.com/community/developer-forum/finding-empty-fields-in-an-table/m-p/1652647
var table = 'incident';
var gr = new GlideRecord(table);
gr.setLimit(1);
gr.query();
gr.next();
var fields = gr.getFields();
for (var i = 0; i < fields.size(); i++) {
var field = fields.get(i).getName();
var empty = isEmpty(table, field);
gs.print('Column: ' + field + ' Empty: ' + empty);
}
function isEmpty(table, column) {
var col = new GlideRecord(table);
col.addNotNullQuery(column);
col.setLimit(1);
col.query();
return !col.hasNext();
}
Regards
Dr. Atul G. - Learn N Grow Together
ServiceNow Techno - Functional Trainer
LinkedIn: https://www.linkedin.com/in/dratulgrover
YouTube: https://www.youtube.com/@LearnNGrowTogetherwithAtulG
Topmate: https://topmate.io/dratulgrover [ Connect for 1-1 Session]
****************************************************************************************************************
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
HI @nikhilagr2024 ,
you can run this script in the background script to find out
var customFields = []
var dGr = new GlideRecord('sys_dictionary');
dGr.addEncodedQuery('name=cmdb_ci^elementSTARTSWITHu_^');
dGr.query();
while (dGr.next()) {
customFields.push(dGr.getValue('element'));
}
var unUsedFields = []
for (var i = 0; i < customFields.length; i++) {
var cmdbGr = new GlideRecord('cmdb_ci');
cmdbGr.setLimit(1)
cmdbGr.addNotNullQuery(customFields[i]);
cmdbGr.query();
if (!cmdbGr.hasNext()) {
unUsedFields.push(customFields[i]);
}
}
gs.info('list of unused fields '+unUsedFields.toString())
Please mark my answer as helpful/correct if it resolves your query.
Regards,
Chaitanya
