|
| 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 | +}); |
0 commit comments