-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathScriptConsoleG.xml
More file actions
512 lines (423 loc) · 14 KB
/
ScriptConsoleG.xml
File metadata and controls
512 lines (423 loc) · 14 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?xml version="1.0" encoding="UTF-8"?>
<unload unload_date="2026-01-25 11:59:21">
<sys_script_include action="INSERT_OR_UPDATE">
<access>public</access>
<active>true</active>
<api_name>global.ScriptConsoleG</api_name>
<caller_access/>
<client_callable>false</client_callable>
<description>Global dependencies for the script console application.</description>
<mobile_callable>false</mobile_callable>
<name>ScriptConsoleG</name>
<sandbox_callable>false</sandbox_callable>
<script><![CDATA[var ScriptConsoleG = Class.create();
ScriptConsoleG.prototype = {
initialize: function () {
this.$sp = new global.GlideSPScriptable();
},
getGlideForm: function (table, guid, qry, view) {
return this.$sp.getForm(table, guid, qry, view);
},
getUserDateFormat: function () {
return gs.getSession().getUser().getDateFormat();
},
getGlobalGr: function (table, initialise, workflow, sysFields, secure) {
const gr = secure ? new GlideRecordSecure(table) : new GlideRecord(table);
if (initialise) gr.initialize();
if (workflow) gr.setWorkflow(false);
if (sysFields) gr.autoSysFields(false);
return gr;
},
getGlobalGa: function (table) {
return new GlideAggregate(table);
},
grMethod: function (gr, fn, params) {
return gr[fn].apply(gr, params);
},
revertVersion: function (versionId) {
return new GlideappUpdateVersion().revert(versionId);
},
getCurrentScope: function () {
return {
value: gs.getCurrentApplicationId(),
name: gs.getCurrentApplicationName()
};
},
setCurrentScope: function (appScope) {
const updated = gs.setCurrentApplicationId(appScope);
if (!updated) return null;
return this.refreshScope();
},
getCurrentUpdateSet: function () {
const setId = new GlideUpdateSet().get();
const grSet = new GlideRecord('sys_update_set');
if (!grSet.get(setId)) return null;
return {
value: j2js(setId),
name: grSet.getDisplayValue(),
state: grSet.getValue('state')
};
},
setCurrentUpdateSet: function (updateSet) {
let updated = false;
const gus = new GlideUpdateSet();
if (!updateSet) {
const scopeDefault = gus.getDefault();
gus.set(scopeDefault);
updated = gus.get() == scopeDefault();
} else {
gus.set(updateSet);
updated = gus.get() == updateSet;
}
return updated ? this.getCurrentUpdateSet() : null;
},
refreshScope: function () {
return {
scope: this.getCurrentScope(),
updateSet: this.getCurrentUpdateSet()
};
},
getSimpleCount(table, query) {
const ga = new GlideAggregate(table);
if (query) ga.addQuery(query);
ga.addAggregate('COUNT');
ga.query();
return ga.next() ? +ga.getAggregate('COUNT') : 0;
},
getUserViaUsername(username) {
const grUser = new GlideRecord('sys_user');
return grUser.get('user_name', username) ? grUser : false;
},
getFormattedDate(gdtValue, format) {
const gdt = new GlideDateTime(gdtValue);
const userDate = gdt.getDisplayValueInternal().split(' ')[0];
const gd = new GlideDate();
gd.setValue(userDate);
if (format === 'display') return gd.getDisplayValue();
return gd.getByFormat(format || 'dd MMM, yyyy');
},
getLastUpdated(gr, format) {
const grUser = this.getUserViaUsername(gr.getValue('sys_updated_by'));
const updated =
format === 'displayTime'
? gr.getDisplayValue('sys_updated_on')
: this.getFormattedDate(gr.getValue('sys_updated_on'));
if (grUser) {
const initials = grUser.getValue('first_name').charAt(0) + grUser.getValue('last_name').charAt(0);
return {
initials,
updated,
guid: grUser.getUniqueValue(),
name: grUser.getDisplayValue('name'),
photo: grUser.getDisplayValue('avatar') || grUser.getDisplayValue('photo')
};
}
return { updated };
},
getScriptData: function (table, guid, field) {
const gr = new GlideRecordSecure(table);
if (!gr.get(guid)) return null;
const scopeChange = this.autoScopeSwitch(gr);
return {
scopeChange,
script: gr.getValue(field) || '',
canWrite: gr[field].canWrite(),
esVersion: this.getEsMode(gr, table, guid),
metadata: {
table,
guid,
field,
scope: gr.sys_scope.name,
display: gr.getDisplayValue(),
updater: this.getLastUpdated(gr, 'displayTime'),
type: gr[field].getED().getInternalType()
}
};
},
autoScopeSwitch: function (gr) {
var scopeChange = this.refreshScope();
if (gs.getPreference('script_console.scope_switch') === 'true') {
const scope = gr.sys_scope.toString();
scopeChange = this.setCurrentScope(scope);
}
return scopeChange || undefined;
},
getListMechanic: function (table, view) {
const lm = new ListMechanic();
lm.setViewName(view || 'Default view');
const sl = lm._getSysList(table);
const cls = sl.getListSet();
lm.applyRules(cls, table);
const cols = j2js(cls.columns).map(c => ({ label: c.label, value: c.value }));
const selected = j2js(cls.selected).map(s => ({ label: s.label, value: s.value }));
return {
unselected: cols,
selected: selected,
isUserList: sl.isUserList()
};
},
setListMechanic: function (table, fields, view) {
lm = new ListMechanic();
lm.setViewName(view || 'Default view');
if (fields) {
lm.saveList(table, fields);
} else {
lm.reset(table);
}
},
getTableData: function (gr, page, pageSize, query) {
const gUser = gs.getUser();
const table = gr.getTableName();
const userTableView = gUser.getPreference(`${table}_list.view`) || '';
let fields = this.getListFields(table, userTableView, gUser.getID());
gr.initialize();
fields = fields
.filter(f => f.element && !f.element.startsWith('.'))
.map(f => ({
name: f.element,
label: gr[f.element].getLabel()
}));
const grRecord = new GlideRecordSecure(table);
if (query) grRecord.addEncodedQuery(query);
grRecord.chooseWindow(page * pageSize, (page + 1) * pageSize);
grRecord.query();
const records = [];
while (grRecord.next()) {
const guid = grRecord.getUniqueValue();
const item = {
sys_id: {
value: guid,
display_value: guid
}
};
fields.forEach(f => {
item[f.name] = {
value: (grRecord.getValue(f.name) || '').substring(0, 200),
display_value: (grRecord.getDisplayValue(f.name) || '').substring(0, 200)
};
});
records.push(item);
}
const config = {
hasTextIndex: this.$sp.hasTextIndex(table),
displayField: grRecord.getDisplayName(),
tableLabel: gr.getClassDisplayValue()
};
return {
fields,
records,
config,
totalCount: this.getSimpleCount(table, query)
};
},
_getListGr: function (table, userTableView, userId) {
const grList = new GlideRecord('sys_ui_list');
grList.addQuery('name', table);
grList.addQuery('view.name', userTableView);
grList.addQuery('sys_user', userId).addOrCondition('sys_user', '');
grList.addNullQuery('parent');
grList.orderByDesc('sys_user');
grList.orderByDesc('sys_created_on');
grList.setLimit(1);
grList.query();
return grList;
},
getListFields: function (table, userTableView, userId) {
let grList = this._getListGr(table, userTableView, userId);
if (!grList.hasNext()) grList = this._getListGr(table, '', userId);
const listId = grList.next() ? grList.getUniqueValue() : '';
if (!listId) {
const grDb = new GlideRecord('sys_db_object');
if (!grDb.get('name', table)) return null;
const parent = grDb.super_class.name.getValue();
if (!parent) return null;
return this.getListFields(parent, userTableView, userId);
}
const grElement = new GlideRecord('sys_ui_list_element');
if (listId) {
grElement.addQuery('list_id', listId);
} else {
grElement.addQuery('list_id.name', table);
grElement.addQuery('list_id.parent', '');
grElement.addQuery('list_id.sys_user', '');
grElement.addQuery('list_id.view.name', '');
}
grElement.orderBy('position');
grElement.query();
let fields = [];
while (grElement.next()) {
fields.push({
element: grElement.getValue('element'),
position: grElement.getValue('position')
});
}
return fields;
},
getReferenceDisplay: function (table) {
var gr = new GlideRecord(table);
return gr.getDisplayName();
},
getActivityData: function (table, guid, instance) {
const meta = this.getSpScriptable().getStream(table, guid);
const jFields = meta.journal_fields;
const readable = jFields.filter(f => f.can_read).map(f => f.name);
const writeable = jFields.filter(f => f.can_write).map(f => f.name);
let entries = meta.entries.filter(f => !!f.value);
const userImgMap = {};
const grUser = new GlideRecord('sys_user');
entries = entries.map(e => {
if (userImgMap[e.user_sys_id]) {
e.user_img = userImgMap[e.user_sys_id];
} else if (grUser.get(e.user_sys_id)) {
const userImg = grUser.getDisplayValue('avatar') || grUser.getDisplayValue('photo');
if (!userImg) return e;
const photo = instance + userImg;
e.user_img = photo;
userImgMap[e.user_sys_id] = photo;
}
return e;
});
return {
...meta,
entries,
readable,
writeable
};
},
getSecurity: function (gr, guid) {
var access = {};
if (guid == -1) {
gr.initialize();
var canCreate = gr.canCreate();
access.canRead = canCreate;
access.canWrite = canCreate;
} else {
access.canRead = gr.canRead();
access.canWrite = gr.canWrite();
access.canDelete = gr.canDelete();
}
return access;
},
getAttachments: function (table, guid, instance) {
grAttach = new GlideRecordSecure('sys_attachment');
grAttach.addQuery('table_name', table);
grAttach.addQuery('table_sys_id', 'IN', guid);
grAttach.orderBy('sys_created_on');
grAttach.query();
var attachments = [];
while (grAttach.next()) {
var id = grAttach.getUniqueValue();
attachments.push({
sys_id: id,
url: instance + 'sys_attachment.do?sys_id=' + id,
file_name: grAttach.getValue('file_name'),
content_type: grAttach.getValue('content_type')
});
}
return attachments;
},
getGlideUser: function () {
const gsu = gs.getUser();
return {
roles: j2js(gsu.getAllRoles()),
departmentID: gsu.getDeparmentID(),
firstName: gsu.getFirstName(),
lastName: gsu.getLastName(),
fullName: gsu.getFullName(),
userID: gsu.getID(),
userName: gsu.getName()
};
},
getEsMode: function (gr, table, guid) {
let jsVersion = () => {
let scope = gr.sys_scope;
//First try to get scope via target record
if (scope) {
const esMode = scope.js_level.getValue();
return esMode == 'es_latest' ? 'latest' : 5;
}
//Next try to get scope via table
const grDb = new GlideRecord('sys_db_object');
if (grDb.get('name', table)) {
scope = grDb.sys_scope;
if (scope.toString() != 'global') {
const ecma = grDb.get('name', table) ? scope.js_level.toString() : '5';
return ecma == 'es_latest' ? 'latest' : 5;
}
}
//Lastly get scope via users current application
scope = gs.getCurrentApplicationId();
if (scope == 'global') return 5;
const grScope = new GlideRecord('sys_scope');
if (grScope.get(scope)) {
const esMode = grScope.getValue('js_level');
return esMode == 'es_latest' ? 'latest' : 5;
}
};
const jsv = jsVersion() || 5;
if (guid == -1 || jsv == 'latest') return jsv;
const grEsLatest = new GlideRecord('sys_es_latest_script');
grEsLatest.addQuery('id', guid);
grEsLatest.addQuery('use_es_latest', true);
grEsLatest.query();
return grEsLatest.next() ? 'latest' : jsv;
},
modifyPolicies: function (policies, gr) {
for (const p of policies) {
for (const c of p.conditions) {
if (c.reference_fields) {
c.dotWalkedValue = this.dotwalkElement(gr, c.field);
}
}
}
},
dotwalkElement: function (gr, fields) {
var fieldList = fields.split('.');
fieldList.forEach(f => {
gr = gr[f];
});
return gr.toString();
},
savePolicyActions: function (policyData) {
const inserted = policyData.toInsert.map(action => {
const gr = new GlideRecord('sys_ui_policy_action');
gr.initialize();
Object.keys(action).forEach(field => {
gr[field] = action[field];
});
return gr.insert();
});
const updated = policyData.toUpdate.map(action => {
const gr = new GlideRecord('sys_ui_policy_action');
const targetGuid = action.sys_id;
delete action.sys_id;
if (!gr.get(targetGuid)) return '';
Object.keys(action).forEach(field => {
gr[field] = action[field];
});
return gr.update();
});
if (policyData.toDelete.length) {
const grPolicy = new GlideRecord('sys_ui_policy_action');
grPolicy.addQuery('sys_id', 'IN', policyData.toDelete);
grPolicy.deleteMultiple();
}
return { inserted, updated, deleted: policyData.toDelete };
},
type: 'ScriptConsoleG'
};
]]></script>
<sys_class_name>sys_script_include</sys_class_name>
<sys_created_by>vinceadmin</sys_created_by>
<sys_created_on>2025-09-24 11:40:00</sys_created_on>
<sys_id>e47924f383c8be1025451429feaad34f</sys_id>
<sys_mod_count>179</sys_mod_count>
<sys_name>ScriptConsoleG</sys_name>
<sys_package display_value="Global" source="global">global</sys_package>
<sys_policy/>
<sys_scope display_value="Global">global</sys_scope>
<sys_update_name>sys_script_include_e47924f383c8be1025451429feaad34f</sys_update_name>
<sys_updated_by>vinceadmin</sys_updated_by>
<sys_updated_on>2026-01-25 11:57:47</sys_updated_on>
</sys_script_include>
</unload>