Search Best Practices
Follow these guidelines when implementing searches in the SuiteScript that you develop for your Commerce customizations:
-
Avoid performing a search per individual line item.
Build an array of item records and use that as a filter to execute only one search for all records, instead of performing individual searches per line.
-
Consolidate multiple searches into one search with a broader condition.
This consolidation reduces the number of calls you make. If your script performs multiple searches for the same record, manipulate the returned search result in each of the different cases, rather than performing the search repeatedly.
-
Avoid loading the record of each search result.
After performing a search, do not use nlapiLoadRecord or nlapiLookupField to load the record into memory to retrieve data. This practice is inefficient because of the unnecessary number of I/O API calls. Instead, prior to running the search, add the desired columns to the search and use getValue.
For example, compare this unoptimized script:
// Unoptimized script var filters = new Array(); filters.push(new nlobjSearchFilter('balance', null, 'between', 0, 1000)); var results = nlapiSearchRecord('customer'), null, filters, null); for (var i=0; results != null && results.length < i; i++) { var recId = results[i].getId(); //using search results to help load records in a for loop is very innefficient var customer = nlapiLoadRecord ('customer', recId); var entityID = customer.getFieldValue ('entityid') }
With the following optimized script:
// Optimized script var filters = new Array(); filters.push(new nlobjSearchFilter ('balance', null, 'between', 0, 1000)); // Adding the desired columns in the search gives you the desired data and avoids loading records unnecessarily var columns = new Array(); columns.push(new nlobjSearchColumn('entityid', null, null)); var results = nlapiSearchRecord('customer', null, filters, columns); for (var i=0; results != null && results.length < i; i++) { var entityId = results[i+1].getValue('entityid') }