Scenario: As a new user of NetSuite, you may find yourself needing to generate CSV files for various purposes, such as data export or reporting. One way to achieve this is by using a Suitelet script. In this article, we will walk you through the process of creating a CSV file using a Suitelet script in NetSuite.
What is a Suitelet Script?
A Suitelet script is a type of script in NetSuite that allows you to create custom functionality and interact with the platform's API. Suitelet scripts can be used to perform a wide range of tasks, including data export, data import, and automation of business processes.
To create a CSV file using a Suitelet script, you will first need to create a new script in NetSuite. To do this, follow these steps:
- Step 1: Create a New Script
- Navigate > Customization > Scripting > Scripts > New
- Click (+) icon to upload the script file
- Upload the Script file from Computer and Click Save
- Click Create Script Record
- Scripted Record
- Enter Name and ID for your script
- Click Save & Deploy
- Step 2: Deploying the Script
Script Deployment- Title: Enter Title
- Status: Select Released
- Log Level: Select Debug
- Execute As Role: Select Administrator
- Audience: Internal Roles : Checkmark Select All
- Click Save
- Running the script using URL
- Navigate and Click URL
- Step 3: Locate the file in File Cabinet
- Navigate > Documents > Files > File Cabinet
- Click Folder Specified in the Script
- Click CSV File
Sample Script :
/**
@NApiVersion 2.0 @NScriptType Suitelet @NModuleScope SameAccount
*/
define(['N/file', 'N/search'], function (file, search) {
function createCSVFile(context) {
try {
log.debug({ title: 'Status:', details: 'start' });
// Creating variables that will be populated from the saved search results
var content = new Array();
var csvColumns = new Array();
var lineOne = '';
// Load a Custom Search, by using its Internal ID
var mySearch = search.load({
id: 'customsearch8623'
});
// Run/Execute the loaded Search
var resultSet = mySearch.run();
resultSet.each(function (result) {
var temp = '';
for (var i = 0; i < mySearch.columns.length; i++) {
var searchResult = result.getValue({
name: mySearch.columns[i].name
});
temp += searchResult + ',';
}
content.push(temp);
return true;
});
resultSet.columns.forEach(function (col) {
csvColumns.push(col.label);
});
for (var i = 0; i < csvColumns.length; i++) {
lineOne += csvColumns[i] + ',';
}
// Looping through the content array and assigning it to the contents string variable.
lineOne = lineOne + '\n';
for (var j = 0; j < content.length; j++) {
lineOne += content[j].toString() + '\n';
}
log.debug({ title: 'Status:', details: 'Saving CSV' });
// --- How to Save a New File Every Time
var now = new Date();
var timestamp = now.getFullYear() +
("0" + (now.getMonth() + 1)).slice(-2) +
("0" + now.getDate()).slice(-2) + "_" +
("0" + now.getHours()).slice(-2) +
("0" + now.getMinutes()).slice(-2) +
("0" + now.getSeconds()).slice(-2);
var filename = 'search_csv_file_' + timestamp + '.csv';
var csvFile = file.create({
name: filename,
fileType: file.Type.CSV,
contents: lineOne,
folder: '22679' // Folder ID where the file should be saved in the File Cabinet
});
csvFile.save();
} catch (error) {
log.debug({ title: 'Catch - Error:', details: error });
}
}
return {
onRequest: createCSVFile
}; });
DISCLAIMER: The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
Visit the SuiteCloud section under the New to NetSuite category to explore a range of beginner-friendly guides that walk you through essential accounting features in NetSuite.