Skip to content

Commit 6218cd5

Browse files
authored
Data lookup utils (#888)
* Create DataLookupUtils.js * Create readme.md
1 parent 854130e commit 6218cd5

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
var DataLookupUtils = Class.create();
2+
3+
DataLookupUtils.prototype = {
4+
initialize: function (tableName, sortByColumn) {
5+
this.tableName = tableName;
6+
this.columnNames = this._getColumnNames(tableName);
7+
this.queryColumns = this.columnNames;
8+
9+
sortByColumn = sortByColumn || false;
10+
this.sortColumn = sortByColumn ? DataLookupUtils.SORT_BY_COLUMN : DataLookupUtils.SORT_BY_ORDER; // optional, use the field being retrieved or the built in order field
11+
},
12+
13+
/**
14+
* query lookup data
15+
* can accept either an array of keys, or multiple parameters
16+
* e.g
17+
* getLookupData('key1', 'key2', 'key3');
18+
* getLookupData(['key1', 'key2', 'key3']);
19+
*/
20+
getLookupData: function (_keys) {
21+
var keys = [];
22+
if (_keys && typeof _keys != 'object') {
23+
for (var _arg in arguments) {
24+
keys.push(arguments[_arg]);
25+
}
26+
} else {
27+
keys = _keys;
28+
}
29+
30+
if (keys.length >= this.queryColumns.length) {
31+
NiceError.raise(gs.getMessage("Too many keys ({0}) provided. Maximum is {1}", [keys.length.toString(), (this.queryColumns.length - 1).toString()]));
32+
}
33+
34+
try {
35+
var fieldIdx = 0;
36+
var fieldName = this.queryColumns[fieldIdx];
37+
var gq = new global.GlideQuery(this.tableName)
38+
.where('active', true);
39+
40+
if (keys.length > 0) {
41+
// loop through the keys
42+
keys.forEach(function (key, idx, arr) {
43+
gq = gq.where(fieldName, key);
44+
fieldName = this.queryColumns[++fieldIdx];
45+
}, this);
46+
}
47+
48+
gq = gq.orderBy(this.sortColumn == DataLookupUtils.SORT_BY_COLUMN ? fieldName : 'order')
49+
.whereNotNull(fieldName)
50+
.select(fieldName)
51+
.map(function (_x) {
52+
// just need the data, not the other stuff
53+
return _x[fieldName];
54+
})
55+
.reduce(function (arr, e) {
56+
// remove duplicates
57+
if (arr.indexOf(e) == -1) arr.push(e);
58+
return arr;
59+
}, []);
60+
61+
return gq;
62+
} catch (e) {
63+
NiceError.raise(e);
64+
}
65+
},
66+
67+
/**
68+
* override the columns to use for lookups
69+
*
70+
*/
71+
setColumns: function (columns) {
72+
if (columns.length == 0) return;
73+
74+
this.queryColumns = [];
75+
columns.forEach(function (_col) {
76+
if (this.columnNames.indexOf(_col) > -1) {
77+
this.queryColumns.push(_col);
78+
} else {
79+
NiceError.raise(gs.getMessage('Cannot find column {0}. Valid columns are {1}', [_col, this.columnNames.join(', ')]));
80+
}
81+
}, this);
82+
83+
},
84+
85+
/**
86+
* build the list of column names for the table
87+
* use the default system view for the table and exclude all system and inherited fields
88+
* column order determines key lookup
89+
*/
90+
_getColumnNames: function () {
91+
return new global.GlideQuery('sys_ui_list_element')
92+
.where('list_id.name', this.tableName)
93+
.where('list_id.view', 'Default view')
94+
// ignore any system or inherited fields from the table
95+
.where('element', 'NOT IN', ['sys_class_name', 'sys_created_by', 'sys_created_on', 'sys_id', 'sys_mod_count', 'sys_name', 'sys_package', 'sys_policy', 'sys_scope', 'sys_updated_by', 'sys_updated_on', 'sys_update_name', 'active', 'order'])
96+
.whereNull('list_id.sys_user') // make sure we get the system view
97+
.orderBy('position')
98+
.select('element')
99+
.reduce(function (arr, e) {
100+
arr.push(e.element);
101+
return arr;
102+
}, []);
103+
},
104+
105+
type: 'DataLookupUtils'
106+
};
107+
108+
DataLookupUtils.SORT_BY_ORDER = 0;
109+
DataLookupUtils.SORT_BY_COLUMN = 1;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Data Lookup Table Utils
2+
3+
This script include provides a quick method of looking up data from any table extended from dl_matcher (Data Lookup Matcher Rules).
4+
5+
It will build a list of the columns for the specified table and then allow you to query based on an array of values.
6+
7+
For example, using the dl_u_priority table we could lookup the Urgency values for a given Impact as follows;
8+
9+
```javascript
10+
var lib = new global.DataLookupUtils("dl_u_priority")
11+
12+
var lookupData = lib.getLookupData("1");
13+
gs.info(lookupData);
14+
```
15+
This will return *1,2,3*.
16+
17+
By also passing in a second value we can filter on Urgency and Impact;
18+
19+
```javascript
20+
var lib = new global.DataLookupUtils("dl_u_priority")
21+
22+
var lookupData = lib.getLookupData(["1", "2"]);
23+
gs.info(lookupData);
24+
```
25+
This will return *2*.
26+
27+
We could also ignore the Impact column and lookup Priority for a given Urgency by setting our own lookup columns;
28+
29+
```javascript
30+
var lib = new global.DataLookupUtils("dl_u_priority")
31+
lib.setColumns(["urgency", "priority"]);
32+
33+
var lookupData = lib.getLookupData(["3"]);
34+
gs.info(lookupData);
35+
```
36+
This will return *3,4,5*
37+

0 commit comments

Comments
 (0)