Skip to content

Commit 62f449b

Browse files
ajcooper72Lacah
andauthored
Get field values ajax (ServiceNowDevProgram#871)
* Create MRVSUtils.js UI Script holding observer class * Create readme.md * Create GetFieldValuesAjax.js * Create readme.md * Delete UI Scripts/Observe MRVS Events/readme.md I believe you didn't intend to include this file in this PR * Delete UI Scripts/Observe MRVS Events/MRVSUtils.js I believe you didn't intend to include this file in this PR --------- Co-authored-by: Laszlo <[email protected]>
1 parent a110d1b commit 62f449b

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
var GetFieldValuesAjax = Class.create();
2+
GetFieldValuesAjax.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3+
4+
getValues: function() {
5+
var table = this.getParameter("sysparm_table_name");
6+
var sys_id = this.getParameter("sysparm_sys_id");
7+
var fields = '' + this.getParameter("sysparm_field_names");
8+
9+
return JSON.stringify(
10+
this._getValues(table, sys_id, fields.split(','))
11+
);
12+
},
13+
14+
/**SNDOC
15+
@name _getValues
16+
@private
17+
@description
18+
Get the requested values from the table/record specified.
19+
Uses GlideRecord to allow for dot-walking and top-level values from the same field
20+
@param {string} [table_name] - table to get record from
21+
@param {string} [record_sys_id] - sys_id of record to query
22+
@param {array} [field_names] - list of field names to retrieve
23+
@returns {JSON} JSON object containing each field and its display equivalent
24+
@example
25+
_getValues('sys_user', '62826bf03710200044e0bfc8bcbe5df1', ['user_name', 'first_name', 'last_name', 'email', 'manager.email' ])
26+
*/
27+
_getValues: function(table, sys_id, fields) {
28+
var json = {};
29+
try {
30+
31+
var gr = new GlideRecord(table);
32+
gr.get(sys_id);
33+
34+
fields.forEach(function(_field) {
35+
var ge = gr.getElement(_field);
36+
if (ge) {
37+
json[_field] = {
38+
"value": ge.getValue(),
39+
"displayValue": ge.getDisplayValue()
40+
};
41+
}
42+
});
43+
return json;
44+
} catch (e) {
45+
gs.error("(GetFieldValuesAjax) Problem getting field values " + e.toString());
46+
return {};
47+
}
48+
},
49+
50+
type: 'GetFieldValuesAjax'
51+
});

GlideAjax/Get Field Values/readme.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# Get Field Values
2+
3+
Simple GlideAjax script to get field values from a specified record. Can query fields including dot walked.
4+
Returns value and displayValue as JSON object.
5+
6+
```json
7+
{
8+
"number": {
9+
"value": "INC0010051",
10+
"displayValue": "INC0010051"
11+
},
12+
"caller_id.email": {
13+
"value": "[email protected]",
14+
"displayValue": "[email protected]"
15+
},
16+
"short_description": {
17+
"value": "New Ticket",
18+
"displayValue": "New Ticket"
19+
},
20+
"state": {
21+
"value": "2",
22+
"displayValue": "In Progress"
23+
},
24+
"priority": {
25+
"value": "1",
26+
"displayValue": "1 - Critical"
27+
}
28+
}
29+
```
30+
31+
32+
```javascript
33+
function onLoad() {
34+
35+
var ga = new GlideAjax("GetFieldValuesAjax");
36+
ga.addParam('sysparm_name', 'getValues');
37+
ga.addParam('sysparm_table_name', 'incident');
38+
ga.addParam('sysparm_sys_id', '915a93a9473d751001612c44846d4367');
39+
ga.addParam('sysparm_field_names', 'number,caller_id.email,short_description,state,priority');
40+
ga.getXMLAnswer(function(_answer) {
41+
var json = JSON.parse(_answer);
42+
console.log(JSON.stringify(json, '', 3));
43+
})
44+
45+
}
46+
```
47+

0 commit comments

Comments
 (0)