Free Code – Wrapper for searches in NetSuite
About a year ago I wrote a SuiteScript 1.0 class as a wrapper around the search functionality in NetSuite. I have updated the code over time, and I want to share the latest version. Among the new features is support for formulas and search expressions. The class should be backwards compatible with the original version, but in addition you can also pass an object to most functions, instead of passing separate parameters. This makes it more flexible and allows me to add more functionality. Enjoy! /** * Encapsulate NetSuite search functionality in an easy-to-use object for SuiteScript 1.0. * * Version Date Author Remarks * 1.0 11 Nov 2016 kmartinsson Initial version * 1.5 06 Jul 2017 kmartinsson Added record type to constructor * 2.0 23 Aug 2017 kmartinsson Added Search2 function, with support for objects and adding multiple columns/filters * 2.0.1 01 Sep 2017 kmartinsson Bug-fixes * 2.0.2 01 Sep 2017 kmartinsson Fixed issue with join not being null, added hasOwnProperty check * 3.0 20 Nov 2017 kmartinsson Removed v1.x code stream, renamed Search2 to Search * 3.0.1 06 Dec 2017 kmartinsson Added JSDoc style comments, updated comments to new JSDoc style * 3.0.2 28 Feb 2018 kmartinsson Fixed bug in sort key which prevented proper sorting. Added alternative keys. * 3.0.3 15 Jul 2018 kmartinsson Added filter expression support * 3.0.4 01 Oct 2018 kmartinsson Added method removeColumns() for use on (external) saved search * */ /** * Search object * @constructor * @param {string} recordtype - Optional NetSuite recordtype (internalid) */ function Search(recordtype) { this.recordType = null; this.columns = []; this.filters = []; this.filterExpressions = []; // Set internal id of saved search to null this.internalId = null; this.noSavedColumns = false; // If record type/ID is supplied, set it now, otherwise default to null if (recordtype != null && recordtype != "") { this.recordType = recordtype; } // Helper function to verify the value is empty or null function isNullOrEmpty(val) { if (val == null || val == '' || val ==[] || val == {}) { return true; } else { return false; } } /** * Remove all columns included in the search * @param none * */ this.removeColumns = function() { this.noSavedColumns = true; } /** * Add a column to include in the search * @param {object}|{string} column - Object specifying a column to return or string containing columnId * @param {string} join - Joined record (internalid) (optional) * @param {boolean}|{string} sorting - Sorting (optional) * Options: true = descending, false = ascending, empty/null = no sorting, "yes" (ascending), * "no", "ascending", "descending" (can be abbreviated "a" and "d" respectively). */ this.addColumn = function(column, join, sorting) { var nsSearchColumn = null; var paramColName = null; var paramJoin = null; var paramSummary = null; var paramSorted = null; // Check if first argument is string or object if (typeof column == "string") { paramColName = column; // Check if second argument is null (for no join) if (isNullOrEmpty(join)) { paramJoin = null; // Check…