Ever thought about CSV export buttons present in NetSuite UI near to saved searches?
What about exporting a saved search result to a CSV file through your code (Suite Script)? Actually this is very simple. Following are the steps that you will need to take:
- Execute Saved Search
- Iterate through saved search results
- Get Value for each column
- Concatenate values of each column separating them by new line
Isn’t that cool? Let’s see it in action!
[code lang=”js”]
/**
* Saved Search results to CSV
* @type {{convertToCSV}}
*/
var SavedSearchResultConverter = (function () {
return {
/**
* Executes given saved search and converts its result to your separator separated values
* @param searchId The search to execute
* @param includeHeader true / false to tell if headers should be included or not
* @returns {*}
*/
convertToCSV: function (searchId, includeHeader) {
var contents = ”;
var searchResult = nlapiSearchRecord(null, searchId, null, null);
if (!searchResult || searchResult.length <= 0) {
throw new Error(‘No result returned after executing search.’);
}
// Creating some array’s that will be populated from the saved search results
var content = [];
var temp = [];
var x = 0;
if (includeHeader === true) {
var firstResult = searchResult[0];
// Returns an array of column internal Ids
var basicColumns = firstResult.getAllColumns();
for (var c = 0; c < basicColumns.length; c++) {
temp[c] = basicColumns[c].getName();
}
content[x] = temp;
x++;
}
temp = [];
// Looping through the search Results
for (var i = 0; i < 10; i++) {
var resultSet = searchResult[i];
// Returns an array of column internal Ids
var columns = resultSet.getAllColumns();
// Looping through each column and assign it to the temp array
for (var y = 0; y < columns.length; y++) {
temp[y] = (!!resultSet.getValue(columns[y]) ? resultSet.getValue(columns[y]) : resultSet.getText(columns[y]));
}
// Taking the content of the temp array and assigning it to the Content Array.
content[x] = temp;
x++;
}
// Looping through the content array and assigning it to the contents string variable.
for (var z = 0; z < content.length; z++) {
contents += content[z].toString() + ‘n’;
}
return contents;
}
};
})();
[/code]
Enjoy!
For reference please check Suite Answers # 36206
FAQs
What is NetSuite search export functionality?
The NetSuite search export feature helps users to extract specific data based on accertain criteria from their NetSuite account. This can be very useful for analyzing data externally or sharing information with partners who do not have access to NetSuite.
How can I perform a NetSuite search export?
To perform a NetSuite search export, you would have to navigate to the relevant section within your NetSuite account, set the search criteria to filter the data, and then select the ‘Export’ option. Choose the desired file format for the export and then follow the prompts to complete the process.
What types of data can be exported using NetSuite search export?
Virtually any data that is stored in NetSuite can be exported using the search export feature, including but not limited to data like sales transactions, customer data, financial records, and inventory information.