Using the “N/file” SuiteScript Module

NetSuite for Developers

The “N/file” module enables developers to create, copy, load, or delete files in the NetSuite File Cabinet.

Why Should You Use this Module?

The “N/file” module can come in handy for certain situations. In fact, it is especially useful for larger development projects. A file stores data in a text format. This is not to be confused with a custom record, which can store data in a highly-customizable and easily-retrievable way. You will need to decide which option is best for your situation.

One use case for the File Module, for example, is to display large amounts of data from a script’s log. NetSuite’s execution log page will limit any results to 3999 characters in the details column according to their documentation. One way to get around this is to create a temporary file to store all your debugging data.

As you work on different projects, you will from time to time find the “N/file” module to be quite useful!

Create and Copy Files

Create Files

First, let’s learn how to create a file.

var fileObject = file.create({
    name: 'testing.txt',
    fileType: file.Type.PLAINTEXT,
    contents: 'Hello, World!',
    folder: 30
});

The only two required parameters are the name and fileType. This will just create a blank file.

But we can also add contents to the file (e.g., “Hello, world!”). You will likely put some kind of variable in your script for this parameter.

Another common option is to specify the folder internal ID, and you can find the ID for a folder by looking at the folder in the NetSuite File Cabinet.

There are many other parameters that can be used, so be sure to check out the help docs!

Copy Files

And copying a file in SuiteScript is even easier! All you need is the file ID and the folder ID.

newFileObject = file.copy({
    id: id,
    folder: -4,
});

Load Files

If you want to load an existing file and manipulate it in some way, there is a SuiteScript method for that as well.

var fileObject = file.load({
    id: 'Images/testImageFile.jpg' // use path or internal ID
});

fileObject.description = 'my test file';
var fileId = fileObject.save();

All you need is a value for the ID parameter. This can either be the precise internal ID of the file, or the file path to the file in the file cabinet (like in the above example).

Delete Files

And finally, there is also a way to delete files in the file cabinet.

var fileObject = file.create({
    name: 'testing.txt',
    fileType: file.Type.PLAINTEXT,
    contents: 'Hello, World!',
    folder: 30
});
var fileId = fileObject.save();

file.delete({ id: fileId }); // <— here

Once again, all you need here is the ID of the file.

Conclusion

We hope you see that creating a file in SuiteScript is quite simple. So many things can be done with this great SuiteScript module!

At SuiteRep, we focus on all things NetSuite so you can focus on better business results. Contact us today to learn how we can partner with you.