This article is relevant if you wish to have NetSuite custom records enforce a unique name or id.
Background
Often times, as we create innovations on the NetSuite platform, we want to specify that the “name” field be unique. We know that NetSuite maintains a unique identifier, the ‘internalid’ in the application. But this identifier is an integer and may not reflect our need for a unique string.
The “Name” field is important. When other records link to the custom record, the Name field is presented to the user. For example, consider a case where you want to maintain a unique list of automobile license plate numbers. NetSuite’s built-in custom Numbering manager is not going to work because the identifiers for license plate numbers are produced independent of NetSuite.
Use SuiteScript to Enforce a Uniqueness
Below is a code pattern that can be deployed to the Before Submit User Event on any custom record to enforce uniqueness.
/**
* Module Description * * Version Date Author * 1.00 20150303 Marty Zigman * * Purpose: General utility to force the "name" of a NetSuite custom record type to be unique * */ function beforesubmit_customrecord_unique_name(type) { var func = " beforesubmit_customrecord_unique_name"; nlapiLogExecution('DEBUG', func + 'Loading with type' , type); if (type == 'delete'){ return; }; var rectype = nlapiGetRecordType(); var newrec = nlapiGetNewRecord(); var recid = newrec.getId(); var recname = newrec.getFieldValue('name') ; nlapiLogExecution('DEBUG', func + 'rectype | newrec.name | newrec.getId' , rectype + ' | ' + recname + ' | ' + recid); //we always expect a name value but better check if (!recname){ return; } else { recname = recname.trim(); }; var filters = new Array(); filters[0] = new nlobjSearchFilter('name', null, 'is', recname); //do not return our current record; it's okay to update ourself with the same value if (recid) { filters[1] = new nlobjSearchFilter('internalid', null, 'noneof', recid); }; var rec = nlapiSearchRecord(rectype, null, filters, null); if (rec) { if (rec.length > 0) { var errmsg = 'Insert/Update Error: Duplicate record found named: \'' + recname + '\' at internal id: ' + rec[0].getId(); nlapiLogExecution('ERROR', func + 'duplicate' , errmsg); throw nlapiCreateError(func, errmsg); }; }; return; };
Get Expertise to Enhance NetSuite
Our team loves working with the NetSuite platform because we can invent solutions that help our client’s business generate more revenue and profit. If you want to get more out of your NetSuite investment, contact us.