This Scheduled Script demonstrates a clean and scalable approach to exporting NetSuite Saved Search results into CSV files using SuiteScript 2.1. The primary goal of this implementation is to pre-create CSV files and populate them automatically with saved search data, making the export process reliable, auditable, and easy to extend.
Key Concept: Create First, Populate Later
Instead of directly exporting search results, the script first creates an empty CSV file in a predefined NetSuite folder. This file acts as a destination container for the search results. Once the file is created and saved, a Search Task is submitted, which runs the saved search and writes the results directly into the created CSV file.
How the Script Works
- Centralized Configuration: All saved searches and their corresponding output filenames are defined in a single array. This makes it easy to add or remove exports without changing the core logic.
- CSV File Creation: For each saved search, a CSV file is created using the
N/filemodule with UTF-8 encoding and stored in a fixed export folder. The file is initially empty but ready to receive data. - Search Task Execution: A Search Task (
task.TaskType.SEARCH) is created and linked to: - the saved search ID
- the newly created CSV file ID
- When the task runs, NetSuite automatically writes the search results into the file.
- Error Isolation and Logging: Each search is processed independently, ensuring one failure does not block others. Detailed audit and error logs help with monitoring and troubleshooting.
Why This Approach Is Useful
- Scalable: Easily supports multiple saved searches with minimal code changes.
- Efficient: Leverages NetSuite’s asynchronous search tasks for large data sets.
- Integration-ready: Generated CSV files can be picked up by external systems for sync or reporting.
- Reliable: Explicit file creation ensures predictable file naming and location.
Use Cases
This pattern is ideal for master data synchronization, scheduled exports, and batch integrations, such as syncing customers, employees, and items to an external application.
In short, this script provides a robust blueprint for exporting saved search results to CSV files in a controlled and automation-friendly manner within NetSuite.
/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
* @description Generic framework to export Saved Search results to CSV files
*/
define(['N/log', 'N/task', 'N/file'],
(log, task, file) => {
// Folder where exported CSV files will be stored
const EXPORT_FOLDER_ID = 000000; // Replace with target folder ID
/**
* List of saved searches to export.
* Add or remove entries without changing core logic.
*/
const SEARCH_DEFINITIONS = [
{
searchId: 'customsearch_example_1',
fileName: 'export_1.csv'
},
{
searchId: 'customsearch_example_2',
fileName: 'export_2.csv'
}
];
const execute = () => {
try {
SEARCH_DEFINITIONS.forEach(def => {
try {
// Step 1: Create an empty CSV file
const csvFile = file.create({
name: def.fileName,
fileType: file.Type.CSV,
contents: '',
folder: EXPORT_FOLDER_ID,
encoding: file.Encoding.UTF8
});
const fileId = csvFile.save();
log.audit('CSV File Created', `${def.fileName} | File ID: ${fileId}`);
// Step 2: Run saved search and write results to file
const searchTask = task.create({
taskType: task.TaskType.SEARCH
});
searchTask.savedSearchId = def.searchId;
searchTask.fileId = fileId;
const taskId = searchTask.submit();
log.audit('Search Task Submitted', `Search: ${def.searchId} | Task ID: ${taskId}`);
} catch (searchErr) {
log.error('Search Export Failed', {
searchId: def.searchId,
error: searchErr
});
}
});
} catch (e) {
log.error('Scheduled Script Execution Failed', e);
}
};
return { execute };
});