-
Notifications
You must be signed in to change notification settings - Fork 20
/
CatalogUtils.js
115 lines (97 loc) · 3.81 KB
/
CatalogUtils.js
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
var CatalogUtils = Class.create();
/**
* Helper class for dealing with catalogs and catalog items.
* All methods can be called both server-side and client-side.
*
* @class CatalogUtils
* @author Maik Skoddow
* @see https://www.servicenow.com/community/now-platform-articles/clone-requested-items/ta-p/2323321
*/
CatalogUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
/**
* Returns all variables of a given RITM as stringified array of JSON objectss.
*
* @param {Object}
* objParameter Valid `sc_req_item` record or String-based Sys ID of a RITM.
* @return {String}
* Stringified array of JSON objects with various variable values and information or an
* empty array if no valid data could be found.
*/
getVariables: function(objParameter) {
var _grRITM = null;
//server-side call with Sys ID
if (typeof objParameter == 'string' && objParameter.length == 32) {
_grRITM = new GlideRecord('sc_req_item');
if (!_grRITM.get(objParameter)) {
_grRITM = null;
}
}
//server-side call with initialized RITM GlideRecord
if (typeof objParameter == 'object' && objParameter instanceof GlideRecord) {
if (objParameter.isValidRecord() && objParameter.getTableName() == 'sc_req_item') {
_grRITM = objParameter;
}
}
//client-side Ajax call with RITM Sys ID as parameter
if (this.getParameter('sysparm_ritm_sys_id')) {
var _strSysID = String(this.getParameter('sysparm_ritm_sys_id')).trim();
if (_strSysID.length == 32) {
_grRITM = new GlideRecord('sc_req_item');
if (!_grRITM.get(_strSysID)) {
_grRITM = null;
}
}
}
//no valid RITM could be loaded
if (_grRITM == null) {
return '[]';
}
var _arrResult = [];
var _strExcludedGlideElementVariableTypes = '|11|12|14|15|17|19|20|24|';
var _grGlideElementVariables = new GlideRecord('sc_item_option_mtom');
var _objGlideElementVariables = {};
//load all catalog variables for a given RITM
_grGlideElementVariables.addQuery('request_item', _grRITM.getValue('sys_id'));
_grGlideElementVariables.query();
while (_grGlideElementVariables.next()) {
var _strType = _grGlideElementVariables.sc_item_option.item_option_new.type.toString();
var _strName = _grGlideElementVariables.sc_item_option.item_option_new.name.toString();
var _strQuestionText = _grGlideElementVariables.sc_item_option.item_option_new.question_text.toString();
var _strValue = _grGlideElementVariables.sc_item_option.value.toString();
var _strDisplayValue = _grRITM.getDisplayValue('variables.' + _strName);
//if type is not excluded fill result array with variable values
if (_strExcludedGlideElementVariableTypes.indexOf('|' + _strType + '|') == -1) {
_objGlideElementVariables[_strName] = {
strType: _strType,
strName: _strName,
strQuestionText: _strQuestionText,
strValue: _strValue,
strDisplayValue: _strDisplayValue,
};
}
}
for (var _strKey in _grRITM.variables) {
var _objVariable = _grRITM.variables[_strKey];
var _strVariableType = Object.prototype.toString.call(_objVariable).match(/^\[object\s(.*)\]$/)[1];
//is it a real catalog variable?
if (_strVariableType == 'GlideElementVariable') {
//is the catalog variable available?
if (_objGlideElementVariables[_strKey]) {
_arrResult.push(_objGlideElementVariables[_strKey]);
}
}
//is it a MVRS?
if (_strVariableType == 'TableVariableNode') {
_arrResult.push({
strType: 'MVRS',
strName: _strKey,
strQuestionText: _grRITM.variables[_strKey].getLabel(),
strValue: _grRITM.variables[_strKey].toString(),
strDisplayValue: null,
});
}
}
return JSON.stringify(_arrResult, '', 4);
},
type: 'CatalogUtils',
});