Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions GlideRecord/isValidGlideRecord/isValidGlideRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Tests whether passed value in parameter `grRecordInstance` represents a valid GlideRecord instance.
*
* @param {Object} grRecordInstance
* Reference to an instance of a GlideRecord object.
* @param {Object} [strTableName]
* If specified an additional check of the GlideRecord against the given table name is performed.
* @returns {Object}
* Name of the table for which the GlideRecord was instantiated.
* @throws TypeError
* In case value of parameter `strTableName` is not a string or parameter count is 0.
*/
function isValidGlideRecord(grRecordInstance, strTableName) {

if (arguments.length === 0) {
throw new TypeError('At least one parameter value is expected!');
}

if (arguments.length > 1 && (typeof strTableName !== 'string' || strTableName.length === 0)) {
throw new TypeError('Value of parameter "strTableName" does not represent a valid string!');
}

var _strTableName = String(strTableName || '').trim();

var _isValid =
grRecordInstance &&
typeof grRecordInstance === 'object' &&
grRecordInstance instanceof GlideRecord &&
!grRecordInstance.isNewRecord() &&
grRecordInstance.isValidRecord();


if (_strTableName.length > 0) {
_isValid = _isValid && grRecordInstance.getTableName() === strTableName;
}

return _isValid;
};
13 changes: 13 additions & 0 deletions GlideRecord/isValidGlideRecord/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Description

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.

# Usage

```
var grIncident = new GlideRecord('incident');

grIncident.get('12345678901234567890123456789012');

gs.info(isValidGlideRecord(grIncident, 'incident'));
```