The N/file module provides complete control over files in the NetSuite File Cabinet β create, read, write, update, and delete files of any type including CSV, PDF, JSON, XML, and plain text.
Loading an Existing File
define(["N/file", "N/log"], (file, log) => {
const fileObj = file.load({ id: 1234 }); // Load by internal ID
const content = fileObj.getContents();
log.debug("File contents", content);
return {};
});
Creating a New File
const newFile = file.create({
name: "output.txt",
fileType: file.Type.PLAINTEXT,
contents: "Hello from SuiteScript!",
folder: 12345 // Folder ID in File Cabinet
});
const fileId = newFile.save();
log.debug("File saved with ID", fileId);
Creating a CSV File
const csv = file.create({
name: "export.csv",
fileType: file.Type.CSV,
contents: "Name,Email,Phone\nJohn,john@test.com,555-1234",
folder: 12345
});
csv.save();
Key Methods
| Method | Description |
|---|---|
file.load(options) | Load an existing file by ID or path |
file.create(options) | Create a new file in memory |
file.delete(options) | Delete a file by ID |
fileObj.getContents() | Get file contents as a string |
fileObj.save() | Save the file to File Cabinet |
fileObj.appendLine(options) | Append a line to the file |