File Object Members in SuiteScript for NetSuite
File object members in SuiteScript enable efficient file handling in NetSuite, including upload, copy, and read operations.
TL;DR Opening
The File Object Members in SuiteScript provide essential tools for managing files within NetSuite, including uploading, copying, and reading files. This functionality is critical for developers needing to manipulate file records efficiently within their SuiteScript applications.
What is the N/file Module?
The N/file module in NetSuite allows developers to work directly with files stored in the NetSuite File Cabinet. It includes capabilities to upload files, manage attachments, and handle file conflicts when duplicates arise. Understanding how to implement these features effectively can greatly enhance the performance and robustness of your NetSuite applications.
Key Features of the N/file Module
- File Uploads: Upload files directly to the File Cabinet.
- Attachment Handling: Send files as attachments without uploading them.
- Copying Files: Duplicate existing files while managing naming conflicts.
- File Reading: Read file contents and iterate over lines easily.
Best Practices for File Management
When dealing with file uploads, it’s vital to adhere to certain best practices:
- Avoid Duplicates: Include unique elements like timestamps or random numbers in file names to prevent overwrites.
- Monitor File Size: The maximum in-memory file size for loading contents is 10 MB; for larger files, consider streaming methods.
- Performance Optimization: If performance slows due to file size during uploads, consider splitting files into smaller segments.
Methods and Properties of the File Object
The following is a summary of key members of the file.File object within the N/file module:
| Member Name | Return Type / Value Type | Description |
|---|---|---|
File.appendLine(options) | file.File | Adds a new line to the end of a CSV or text file. |
File.getContents() | string | Retrieves the content of a file as a string. |
File.lines.iterator() | boolean | Calls a function for each line, stopping when specified. |
File.resetStream() | void | Resets the file stream to its previous state. |
File.save() | number | Saves a new or updated file in the File Cabinet. |
File.getReader() | Object | Returns a reader for file operations. |
File.getSegments(options) | Object | Retrieves an iterator of delimited segments. |
Reader Object Members
For special read operations, the file.Reader object can be utilized. Here are its members:
| Member Name | Return Type / Value Type | Description |
|---|---|---|
Reader.readChars(options) | string | Reads a specified number of characters from the current position. |
Reader.readUntil(options) | string | Reads until a specified tag. |
Example Code Snippet
The following code snippet demonstrates how to use the getReader() method to read from a file:
var reader = context.input.file.getReader();var textUntilFirstComma = reader.readUntil(',');var next10Characters = reader.readChars(10);var textUntilNextNewLine = reader.readUntil('\n');var next100Characters = reader.readChars(100);Who This Affects
- Developers: Implementing file management functions.
- Administrators: Setting up file access controls and management policies.
- Custom App Builders: Creating solutions that require file manipulation within NetSuite.
Key Takeaways
- The N/file module is vital for file management in SuiteScript applications.
- Adhere to best practices for file naming and handling.
- The module provides flexible methods for reading, appending, and saving files.
Frequently Asked Questions (4)
Does the N/file module support both uploading and copying files in NetSuite?
Are there any best practices for avoiding file duplicates when using the N/file module?
What can I do if the file content size surpasses the 10 MB in-memory limit?
How do I iterate over lines of a file using the N/file module in SuiteScript?
Was this article helpful?
More in SuiteScript
- SuiteScript 2.1 Enhancements in NetSuite February Updates
SuiteScript 2.1 now supports async features and PATCH method. Discover the latest API and SuiteProcurement improvements.
- Scheduling Map/Reduce Script Deployments in NetSuite
Learn to schedule map/reduce script submissions, including one-time and recurring options in NetSuite.
- Binary File Support in N/https Module for SuiteScript
SuiteScript enhances capabilities with binary file support in the N/https module, allowing improved data handling in external communications.
- API Governance Units Calculation in NetSuite 2026.1
NetSuite 2026.1 introduces examples illustrating API governance unit calculations for both user event and scheduled scripts.
