forked from ServiceNowDevProgram/code-snippets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartData.scriptinclude.js
More file actions
178 lines (165 loc) · 5.3 KB
/
SmartData.scriptinclude.js
File metadata and controls
178 lines (165 loc) · 5.3 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* Name: SmartData
* Type: Script Include (server-side, global)
* Accessible from: Server scripts (NOT client-callable)
* Author: Abhishek
* Summary: A tiny data helper that auto-picks GlideAggregate for counts/stats/distinct
* and GlideRecord for lists/one. Also includes describe() and preview().
*/
var SmartData = Class.create();
SmartData.prototype = {
initialize: function () {},
/**
* Unified entry
* opts = {
* table: 'incident',
* query: 'active=true^priority=1',
* want: 'list' | 'one' | 'count' | 'distinct' | 'stats' | 'describe' | 'preview',
* fields: ['number','short_description'],
* limit: 50,
* orderBy: 'sys_created_on' | '-sys_created_on',
* field: 'assignment_group', // for distinct
* groupBy: ['assignment_group','priority'], // for stats
* aggregate: { fn:'AVG'|'SUM'|'MIN'|'MAX'|'COUNT', field:'time_worked' }
* }
*/
query: function (opts) {
opts = opts || {};
var want = (opts.want || "list").toLowerCase();
if (want === "count") return this.count(opts.table, opts.query);
if (want === "distinct")
return this.distinct(opts.table, opts.field, opts.query);
if (want === "stats")
return this.stats(opts.table, opts.aggregate, opts.groupBy, opts.query);
if (want === "one")
return this.one(opts.table, opts.query, opts.fields, opts.orderBy);
if (want === "describe") return this.describe(opts.table);
if (want === "preview") return this.preview(opts.table, opts.query);
return this.list(
opts.table,
opts.query,
opts.fields,
opts.limit,
opts.orderBy
);
},
/** Fast COUNT via GlideAggregate */
count: function (table, encQuery) {
var ga = new GlideAggregate(table);
if (encQuery) ga.addEncodedQuery(encQuery);
ga.addAggregate("COUNT");
ga.query();
return ga.next() ? parseInt(ga.getAggregate("COUNT"), 10) || 0 : 0;
},
/** DISTINCT values of a single field (GlideAggregate groupBy) */
distinct: function (table, field, encQuery) {
if (!field) return [];
var ga = new GlideAggregate(table);
if (encQuery) ga.addEncodedQuery(encQuery);
ga.groupBy(field);
ga.addAggregate("COUNT"); // driver
ga.query();
var out = [];
while (ga.next()) out.push(String(ga.getValue(field)));
return out;
},
/**
* Stats via GA.
* aggregate = { fn:'AVG'|'SUM'|'MIN'|'MAX'|'COUNT', field:'duration' }
* groupBy = ['assignment_group','priority']
*/
stats: function (table, aggregate, groupBy, encQuery) {
var fn =
aggregate && aggregate.fn ? String(aggregate.fn).toUpperCase() : "COUNT";
var fld = (aggregate && aggregate.field) || "sys_id";
var ga = new GlideAggregate(table);
if (encQuery) ga.addEncodedQuery(encQuery);
(groupBy || []).forEach(function (g) {
if (g) ga.groupBy(g);
});
ga.addAggregate(fn, fld);
ga.query();
var out = [];
while (ga.next()) {
var row = {};
(groupBy || []).forEach(function (g) {
row[g] = String(ga.getValue(g));
});
row.fn = fn;
row.field = fld;
row.value = ga.getAggregate(fn, fld);
out.push(row);
}
return out;
},
/** One record via GlideRecord */
one: function (table, encQuery, fields, orderBy) {
var gr = new GlideRecord(table);
gr.addEncodedQuery(encQuery || "");
this._applyOrder(gr, orderBy);
gr.setLimit(1);
gr.query();
if (!gr.next()) return null;
return this._pick(gr, fields);
},
/** List via GlideRecord */
list: function (table, encQuery, fields, limit, orderBy) {
var gr = new GlideRecord(table);
gr.addEncodedQuery(encQuery || "");
this._applyOrder(gr, orderBy);
if (limit) gr.setLimit(limit);
gr.query();
var out = [];
while (gr.next()) out.push(this._pick(gr, fields));
return out;
},
/** Quick schema: field name, label, type, ref, mandatory */
describe: function (table) {
var gr = new GlideRecord(table);
gr.initialize();
var fields = gr.getFields(),
out = [];
for (var i = 0; i < fields.size(); i++) {
var f = fields.get(i),
ed = f.getED();
out.push({
name: f.getName(),
label: ed.getLabel(),
type: ed.getInternalType(),
ref: ed.getReference() || "",
mandatory: ed.getMandatory(),
});
}
return out;
},
/** Tiny peek — returns display value (number/ID) for the first match */
preview: function (table, encQuery) {
var gr = new GlideRecord(table);
gr.addEncodedQuery(encQuery || "");
gr.setLimit(1);
gr.query();
if (gr.next()) return gr.getDisplayValue("number") || gr.getUniqueValue();
return null;
},
// --- helpers ---
_applyOrder: function (gr, orderBy) {
if (!orderBy) return;
if (orderBy.indexOf("-") === 0) gr.orderByDesc(orderBy.substring(1));
else gr.orderBy(orderBy);
},
_pick: function (gr, fields) {
var obj = {};
if (Array.isArray(fields) && fields.length) {
fields.forEach(function (f) {
obj[f] = gr.getDisplayValue(f);
});
} else {
obj.sys_id = gr.getUniqueValue();
if (gr.isValidField("number")) obj.number = gr.getValue("number");
if (gr.isValidField("short_description"))
obj.short_description = gr.getValue("short_description");
}
return obj;
},
type: "SmartData",
};