forked from ServiceNowDevProgram/syntax_macros
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetDataDictionary.js
More file actions
94 lines (85 loc) · 3.81 KB
/
Copy pathgetDataDictionary.js
File metadata and controls
94 lines (85 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
function getFieldsValue(tableNames) {
var nonEmptyFields = [];
var fieldCounts = {};
var dictionaryInfo = {};
// Initialize field counts
var gr = new GlideRecord(tableNames);
gr.query();
if (gr.next()) {
var fields = gr.getFields();
for (var i = 0; i < fields.size(); i++) {
var fieldName = fields.get(i).getName();
fieldCounts[fieldName] = 0;
}
}
// Iterate through each record and count non-empty fields
gr = new GlideRecord(tableNames);
gr.query();
while (gr.next()) {
var fields = gr.getFields();
for (var i = 0; i < fields.size(); i++) {
var fieldName = fields.get(i).getName();
var value = gr.getValue(fieldName);
if (value !== '' && value !== null) {
fieldCounts[fieldName]++;
}
}
}
// Identify fields that are non-empty for any records
for (var field in fieldCounts) {
if (fieldCounts[field] > 0) {
nonEmptyFields.push(field);
}
}
var hierarchyList = new TableUtils(tableNames).getTables();
var arr = hierarchyList.toArray();
var resultString = arr.join(', ');
// Fetch additional dictionary information
var dictGr = new GlideRecord('sys_dictionary');
// Query for dictionary entries related to specific tables
dictGr.addEncodedQuery('nameIN'+resultString);
dictGr.query();
while (dictGr.next()) {
var fieldName = dictGr.getDisplayValue('element');
dictionaryInfo[fieldName] = {
type: dictGr.getDisplayValue('internal_type') || 'undefined',
readOnly: dictGr.getDisplayValue('read_only') == 'true' ? 'Yes' : 'No',
mandatory: dictGr.getDisplayValue('mandatory') == 'true' ? 'Yes' : 'No',
fieldTableName: dictGr.getDisplayValue('name') || 'undefined',
referenceTableName: dictGr.getDisplayValue('reference') || '',
maxLength: dictGr.getValue('max_length') || '',
columnLabel: dictGr.getDisplayValue('column_label') || 'empty'
};
}
var csvRows = [];
// Add header row
csvRows.push('Table Name,Field Name,Field Label,Type,Reference,Max Length,Read Only,Mandatory\r\n');
// Add fields and their dictionary info as rows
for (var i = 0; i < nonEmptyFields.length; i++) {
var fieldName = nonEmptyFields[i];
var dictInfo = dictionaryInfo[fieldName] || {
type: 'undefined',
readOnly: 'undefined',
mandatory: 'undefined',
fieldTableName: 'undefined',
referenceTableName:'',
maxLength:'',
columnLabel:'undefined'
};
csvRows.push(dictInfo.fieldTableName + ',' + fieldName + ','+ dictInfo.columnLabel + ',' + dictInfo.type + ',' + dictInfo.referenceTableName + ',' + dictInfo.maxLength + ',' + dictInfo.readOnly + ',' + dictInfo.mandatory + '\r\n');
}
var gdt = new GlideDateTime();
var fileName = tableNames + ' Non Empty Fields ' + gdt.getDisplayValue() + '.csv';
// Write the CSV to an attachment
var attachment = new Attachment();
var attachmentRec = attachment.write('kb_knowledge', 'a20df6b2978f8e1088b036e71153af62', fileName, 'text/csv', csvRows.join(''));
var att = new GlideRecord('sys_attachment');
att.addQuery('file_name', fileName);
att.addQuery('table_sys_id', 'a20df6b2978f8e1088b036e71153af62');
att.orderByDesc('sys_created_on');
att.query();
if (att.next()) {
return att.sys_id.toString(); // Return the sys_id of the attachment
}
return '';
}