The N/file module is one of the most essential and frequently used modules in SuiteScript 2.1. Whether you need to generate CSV exports, read JSON configuration files, create PDF attachments, or manage documents in the NetSuite File Cabinet, the N/file module is your go-to tool. In this complete guide, we will walk through everything you need to know about using N/file in your NetSuite scripts.
What Is the N/file Module?
The N/file module provides a set of APIs to interact with the NetSuite File Cabinet β NetSuite’s built-in document storage system. Using this module, SuiteScript developers can create new files, load existing files by ID or path, read file contents, update file properties, delete files, and attach files to records. It supports a wide range of file types including plain text, CSV, JSON, XML, PDF, and binary files.
How to Load a File from the File Cabinet
You can load a file using either its internal ID or its full cabinet path. Loading by ID is recommended for performance as it avoids a path lookup.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/file', 'N/log'], (file, log) => {
const onRequest = (context) => {
// Load file by Internal ID
const myFile = file.load({ id: 1234 });
log.debug('File Name', myFile.name);
log.debug('File Type', myFile.fileType);
log.debug('File Size', myFile.size);
// Read the file content
const content = myFile.getContents();
log.debug('File Content', content);
// Load file by path
const fileByPath = file.load({
id: '/SuiteScripts/config/settings.json'
});
const jsonContent = JSON.parse(fileByPath.getContents());
log.debug('JSON Config', JSON.stringify(jsonContent));
context.response.write('File loaded successfully.');
};
return { onRequest };
});
How to Create and Save a New File
Creating a new file with N/file is straightforward. Use the file.create() method, set the file properties, then call file.save() to persist it to the File Cabinet. The save() method returns the internal ID of the newly created file, which you can then use to attach it to records or reference it in other scripts.
/**
* @NApiVersion 2.1
* @NScriptType Suitelet
*/
define(['N/file', 'N/log'], (file, log) => {
const onRequest = (context) => {
// Create a new CSV file
const csvFile = file.create({
name: 'sales_export_' + new Date().toISOString().slice(0,10) + '.csv',
fileType: file.Type.CSV,
contents: 'Name,Amount,Date
Acme Corp,5000,2026-05-17
Global Inc,12000,2026-05-17',
folder: 123, // Internal ID of target folder in File Cabinet
isOnline: false
});
const newFileId = csvFile.save();
log.audit('CSV File Created', 'File ID: ' + newFileId);
context.response.write('File created with ID: ' + newFileId);
};
return { onRequest };
});
How to Update an Existing File
To update a file’s contents, load it first, modify the contents or properties, then call save() again. Note that updating a file replaces its content entirely β there is no append mode in N/file. If you need to append, read the existing contents first, concatenate your new data, then save.
define(['N/file', 'N/log'], (file, log) => {
const onRequest = (context) => {
const existingFile = file.load({ id: 1234 });
// Read existing content and append new data
const existingContent = existingFile.getContents();
const newContent = existingContent + '
NewCorp,8500,2026-05-17';
// Create updated version
const updatedFile = file.create({
name: existingFile.name,
fileType: existingFile.fileType,
contents: newContent,
folder: existingFile.folder
});
// Delete old file and save new one
file.delete({ id: existingFile.id });
const updatedId = updatedFile.save();
log.audit('File Updated', 'New File ID: ' + updatedId);
context.response.write('File updated successfully.');
};
return { onRequest };
});
Supported File Types in N/file
The N/file module supports a comprehensive range of file types via the file.Type enum. The most commonly used types in SuiteScript development include file.Type.PLAINTEXT for .txt files, file.Type.CSV for comma-separated data exports, file.Type.JSON for configuration and data interchange files, file.Type.XMLDOC for XML structured data, file.Type.PDF for generated PDF documents, and file.Type.BINARY for binary data like images or zip archives.
Best Practices When Using N/file
Always check the file size before calling getContents() since large files can consume a significant portion of your script’s memory governance limit. Use file.Type constants instead of hardcoded strings to avoid typos. When creating files in bulk, use a Map/Reduce script rather than a User Event or Client Script to stay within governance thresholds. Store folder IDs in a custom configuration record or script parameter rather than hardcoding them, so your scripts remain portable across sandbox and production environments.
Conclusion
The N/file module is an indispensable part of any serious SuiteScript 2.1 developer’s toolkit. From simple configuration file reads to generating dynamic CSV exports and managing binary attachments, mastering N/file will significantly expand what you can accomplish with NetSuite automation. Have a question about N/file or a use case you would like us to cover? Drop it in the comments or post it in our Q&A community!
Discover more from The NetSuite Pro
Subscribe to get the latest posts sent to your email.
Leave a Reply