Skip to content

Commit 0775165

Browse files
authored
feat: new function returns variables as string (#817)
variablesToText checks record variables & returns Question & answer to string. Returns all Variables with an answer as a formatted string. Can be used to apply question:answers to string fields or notifications Added to CatalogUtils this Script include has similar functionality. Included example use and example output as per existing template.
1 parent 0292946 commit 0775165

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

Script Includes/CatalogUtils/CatalogUtils.js

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ var CatalogUtils = Class.create();
99
*/
1010
CatalogUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
1111
/**
12-
* Returns all variables of a given RITM as stringified array of JSON objectss.
13-
*
14-
* @param {Object} objParameter Valid `sc_req_item` record or String-based Sys ID of a RITM.
15-
* @return {String} Stringified array of JSON objects with various variable values and information or an empty array if no valid data could be found.
16-
*/
12+
* Returns all variables of a given RITM as stringified array of JSON objectss.
13+
*
14+
* @param {Object} objParameter Valid `sc_req_item` record or String-based Sys ID of a RITM.
15+
* @return {String} Stringified array of JSON objects with various variable values and information or an empty array if no valid data could be found.
16+
*/
1717
getVariables: function(objParameter) {
1818
var _grRITM = null;
1919

2020
//server-side call with Sys ID
2121
if (typeof objParameter == 'string' && objParameter.length == 32) {
2222
_grRITM = new GlideRecord('sc_req_item');
23-
23+
2424
if (!_grRITM.get(objParameter)) {
2525
_grRITM = null;
2626
}
27-
}
27+
}
2828

2929
//server-side call with initialized RITM GlideRecord
3030
if (typeof objParameter == 'object' && objParameter instanceof GlideRecord) {
@@ -39,7 +39,7 @@ CatalogUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
3939

4040
if (_strSysID.length == 32) {
4141
_grRITM = new GlideRecord('sc_req_item');
42-
42+
4343
if (!_grRITM.get(_strSysID)) {
4444
_grRITM = null;
4545
}
@@ -83,5 +83,24 @@ CatalogUtils.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
8383
return JSON.stringify(_arrResult);
8484
},
8585

86+
/**SNDOC
87+
@name _variablesToText
88+
@description Parses over the variables of a given record, returning Question Label and Display Value of answers
89+
@param {object} [pRecord] GlideRecord object with variables key
90+
@returns {string} A formatted string of variable questions and corresponding answer
91+
*/
92+
variablesToText: function (pRecord) {
93+
var oVariables = pRecord.variables;
94+
var aPayload = [];
95+
for (key in oVariables) {
96+
var sQuestion = oVariables[key].getLabel();
97+
var sAnswer = oVariables[key].getDisplayValue();
98+
if (JSUtil.notNil(sQuestion) && JSUtil.notNil(sAnswer)) {
99+
aPayload.push(sQuestion + ':\n>>' + sAnswer);
100+
}
101+
}
102+
return aPayload.join('\n\n');
103+
},
104+
86105
type: 'CatalogUtils',
87106
});

Script Includes/CatalogUtils/readme.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,33 @@ var jsonVariables = JSON.parse(gaCatalogUtils.getAnswer() || "");
5151
}
5252
]
5353
```
54+
55+
## Variables to Text
56+
57+
Returns all Variables with an answer as a formatted string.
58+
Useful for 'exporting' question:answers to primitive fields such as description or notifications
59+
Expected use is Server side.
60+
61+
### Example of server-side call
62+
```javascript
63+
var grRITM = new GlideRecord('sc_req_item');
64+
65+
if (grRITM.get('8c135e0647b1b110da816241e36d437e')) {
66+
var jsonVariables = JSON.parse((new CatalogUtils()).variablesToText(grRITM));
67+
}
68+
```
69+
70+
### Example of returned values
71+
```
72+
Copier paper (reams):
73+
>>3
74+
75+
Pens (box of 10):
76+
>>3
77+
78+
Screen wipes (tube of 20):
79+
>>4
80+
81+
Additional requirements:
82+
>>I need this yesterday
83+
```

0 commit comments

Comments
 (0)