ANS-0661 · SUITESCRIPT DEVELOPMENT
How to Programmatically Retrieve Custom List Values in NetSuite SuiteScript 2.x
Learn how to access and map the names and internal IDs of custom list entries using SuiteScript 2.x search modules.
Short answer
To retrieve custom list values in NetSuite SuiteScript 2.x, use the `N/search` module to create a search on the custom list record type. Specify 'name' as a column to get the display value. The results can then be mapped to extract both the name and internal ID of each list entry.
Scenario
NetSuite users often need to programmatically access the values stored within a custom list. Although custom lists do not appear as standard records, their entries can be retrieved and processed using SuiteScript. This is necessary when integrating custom list data into other scripts or workflows.
Solution
Even though a custom list does not behave like a standard record, its values can be retrieved programmatically using the N/search module in SuiteScript 2.x. To obtain a list of custom list values, perform a search on the custom list's internal ID and specify 'name' as a search column to retrieve the display value. The following SuiteScript 2.x code demonstrates how to retrieve and map the names and internal IDs of entries from a custom list, using customlist_ehf_category as an example:
javascript
/**
* @NApiVersion 2.x
* @NModuleScope SameAccount
*/
define(['N/search'], function(search) {
// This code snippet would typically be placed within a SuiteScript 2.x script entry point (e.g., execute, onRequest)
// or a custom module function.
var customListId = 'customlist_ehf_category'; // Replace with your custom list's internal ID
var results = [];
search.create({
type: customListId,
columns: [
search.createColumn({name: 'name'}),
search.createColumn({name: 'internalid'})
]
}).run().each(function(result) {
results.push(result);
return true; // Return true to continue the iteration
});
var names = results.map(function(result) {
return result.getValue({name: 'name'});
});
var ids = results.map(function(result) {
return result.getValue({name: 'internalid'});
});
// The 'names' and 'ids' arrays now contain the respective values from the custom list.
// For demonstration, you might log them:
// log.debug('Custom List Names', names);
// log.debug('Custom List IDs', ids);
});This script first defines the N/search module, then creates and executes a search on the specified custom list type, collecting all results. Finally, it uses the map function to extract the display names and internal IDs into separate arrays.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
