- CALL : (+1) 407-273-1001
- Main Office : (+1) 407-273-1001
- NetSuite NetSuite Customization
- Jan 16
- 4 mins read
NetSuite Customization – Creating a CSV Text file from the data in a Saved Search (Part 2-2)

NetSuite Scripting
User Events which execute on the server in response to an event like a user submitting a record, client scripts which execute on the browser are just two examples of the types of scripts available in NetSuite.
A Scheduled script will be the best option for sending data to Mega Publishing. A scheduled script allows us to set a time that the script will execute. We can decide to let the script run in the evenings after business hours as to not take up needed resources.
All that is needed to start writing the code is an editor. I prefer to use Eclipse, but Windows Notepad or any text editor will do. The file must end in .js. Below is the entire contents of the script file I created CreateTextFile.js
function createFile(){
try{
var searchResults = nlapiSearchRecord(null, ‘customsearch_cust_for_mega_pub’);
var csvBody = ”;
if (searchResults == null || searchResults.length < 1) return;
for (var i=0; i<searchResults.length; i++){
csvBody += searchResults[i].getValue(‘entityid’) + ‘,’;
csvBody += searchResults[i].getValue(’email’) + ‘,’;
csvBody += searchResults[i].getValue(‘phone’) + ‘\n’;
updateCustomer(searchResults[i]);
}
var file = nlapiCreateFile(‘MegaPublishing_Customers.csv’, ‘CSV’, csvBody);
file.setFolder(‘1067’);
nlapiSubmitFile(file);
}catch(e){
nlapiLogExecution(‘Error’, ‘createFile’, ‘Error while creating file – ‘ + e.message);
}
}
function updateCustomer(customerId){
var fields = [‘custentity_sent_to_mega_publishing’];
var values = [‘T’];
nlapiSubmitField(‘customer’, customerId, fields, values, false);
}
The function called createFile is the starting point of the script. The NetSuite scheduled script process will call createFile to kick of the rest of the logic.
If you are familiar with JavaScript you can tell that this file is nothing more than a simple JavaScript application. The main difference is the calls to the NetSuite API. These calls are prefixed with nlapi. NetSuite used to be called NetLedger before it was rebranded to NetSuite. So the nlapi prefixes stand for NetLedger Application program Interface. In short, this is how developers can connect to powerful NetSuite functions to control and customize the way NetSuite behaves.
The first API call is this:
var searchResults = nlapiSearchRecord(null, ‘customsearch_cust_for_mega_pub’);
nlapSearchRecord is the NetSuite API for executing searches. The value customsearch_cust_for_mega_pub is the ID for the saved search we created earlier. This is a powerful feature! Business users can use the NetSuite User Interface to change the query of the saved search and this script can access the results.
The following lines of code loop through the return results of the saved search and creates the text body to be used in the CSV file:
for (var i=0; i<searchResults.length; i++){
csvBody += searchResults[i].getValue(‘entityid’) + ‘,’;
csvBody += searchResults[i].getValue(’email’) + ‘,’;
csvBody += searchResults[i].getValue(‘phone’) + ‘\n’;
updateCustomer(searchResults[i]);
}
The call to the updateCustomer function calls the function that will update the Sent To Mega Publishing check box to true or checked. This will cause this customer to drop out of the results of the saved search and we will never process that customer again.
Now, all that is left is to create the text file and save it in the NetSuite File Cabinet:
var file = nlapiCreateFile(‘MegaPublishing_Customers.csv’, ‘CSV’, csvBody);
file.setFolder(‘1067’);
nlapiSubmitFile(file);
We make another call to the NetSuite API to create the file – nlapiCreateFile. This function accepts the file name, file type and finally the file contents as parameters and returns a file object (nlobjFile) to the script. The script then sets the id of the folder to 1067. In NetSuite, all folders in the file cabinet have a unique Id. 1067 is the id of a folder called Mega Publishing. Then the file is actually created using the nlapiSubmitFile API call.
The next step is to upload this script file to NetSuite and execute it:
This result is the following file being created in the Mega Publishing folder in the File Cabinet:
Which, when opened in excel we can clearly see the two records that were returned by the saved search.
The file can now be download and sent to Mega Publishing. The next step would be to automate the transmission of this file directly to Mega Publishing. NetSuite has the ability to do this as well.
Jeremy McCourt is an content producer in the enterprise software industry that focuses on NetSuite and related cloud-based software solutions.
Related Posts

NetSuite Document Management Explained: The 2023 Guide
Proper document management is an important part of managing your business as it can easily affect everything from compliance to productivity. Growing companies must find a way to efficiently manage and streamline their document management…
- Mar 06
- 4 mins read

NetSuite Consolidated Invoicing: The 2023 Guide For NetSuite Users
Contact us! As businesses grow, managing invoicing can become more complex, especially if there are multiple subsidiaries or transactions involved. To help streamline invoicing processes and ensure accuracy, NetSuite users look for tools to consolidate…
- Feb 15
- 4 mins read
Subscribe to the eMerge Technologies Mailing List
eMerge Technologies Services
Recent Posts
- NetSuite Document Management Explained: The 2023 Guide
- NetSuite Consolidated Invoicing: The 2023 Guide For NetSuite Users
- Case Study: Retail Company Explores Financial Goals
- Financial Planning and Analysis Overview: The Foundation for Successful CFOs
- What is Dell Boomi? The 2023 Guide to a Leading IPaas
Comments (3)
Craig D
May 23, 2016Hi Herb,
Really interesting article. I’ve been looking at doing this for a while. I’m getting an error though, nlapiCreateFile not defined. Any ideas?
Thanks!
Herb
May 23, 2016Hey Craig,
I can’t think of a reason why NetSuite would say that the nlapiCreateFile would be undefined. This is a standard NetSuite function. The complete function is: nlapiCreateFile(name, type, contents). Where are you working in Sandbox or production?
Craig D
May 30, 2016Fixed. I was trying to run client side! Thanks, really good article.
Craig