diff --git a/GlideRecord/isValidGlideRecord/isValidGlideRecord.js b/GlideRecord/isValidGlideRecord/isValidGlideRecord.js new file mode 100644 index 0000000000..07aae1197d --- /dev/null +++ b/GlideRecord/isValidGlideRecord/isValidGlideRecord.js @@ -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; +}; diff --git a/GlideRecord/isValidGlideRecord/readme.md b/GlideRecord/isValidGlideRecord/readme.md new file mode 100644 index 0000000000..8a5b9565a4 --- /dev/null +++ b/GlideRecord/isValidGlideRecord/readme.md @@ -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')); +```