Skip to content

Commit b6e0c3c

Browse files
authored
Is valid GlideRecord (#895)
* Create readme.md * Create create_admin_user.js * Create isValidGlideRecord.js * Create is_valid_GlideRecord.js Initial Commit * Delete GlideRecord/isValidGlideRecord/is_valid_GlideRecord.js * Create readme.md Initial commit
1 parent 4bf8f52 commit b6e0c3c

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Tests whether passed value in parameter `grRecordInstance` represents a valid GlideRecord instance.
3+
*
4+
* @param {Object} grRecordInstance
5+
* Reference to an instance of a GlideRecord object.
6+
* @param {Object} [strTableName]
7+
* If specified an additional check of the GlideRecord against the given table name is performed.
8+
* @returns {Object}
9+
* Name of the table for which the GlideRecord was instantiated.
10+
* @throws TypeError
11+
* In case value of parameter `strTableName` is not a string or parameter count is 0.
12+
*/
13+
function isValidGlideRecord(grRecordInstance, strTableName) {
14+
15+
if (arguments.length === 0) {
16+
throw new TypeError('At least one parameter value is expected!');
17+
}
18+
19+
if (arguments.length > 1 && (typeof strTableName !== 'string' || strTableName.length === 0)) {
20+
throw new TypeError('Value of parameter "strTableName" does not represent a valid string!');
21+
}
22+
23+
var _strTableName = String(strTableName || '').trim();
24+
25+
var _isValid =
26+
grRecordInstance &&
27+
typeof grRecordInstance === 'object' &&
28+
grRecordInstance instanceof GlideRecord &&
29+
!grRecordInstance.isNewRecord() &&
30+
grRecordInstance.isValidRecord();
31+
32+
33+
if (_strTableName.length > 0) {
34+
_isValid = _isValid && grRecordInstance.getTableName() === strTableName;
35+
}
36+
37+
return _isValid;
38+
};
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Description
2+
3+
Many developers are building functions which expect any GlideRecord reference as a function parameter but within these functions they do no check whether the passed value really represents a valid GlideRecord. However to test a function parameter for a valid GlideRecord reference is pretty tricky and extensive. Therefore I built a small helper function `isValidGlideRecord` which will do the job. As a second optional parameter you can specify a table name which will be considered for an additional validation.
4+
5+
# Usage
6+
7+
```
8+
var grIncident = new GlideRecord('incident');
9+
10+
grIncident.get('12345678901234567890123456789012');
11+
12+
gs.info(isValidGlideRecord(grIncident, 'incident'));
13+
```

0 commit comments

Comments
 (0)