ANS-1514 · SUITESCRIPT DEVELOPMENT

How to Programmatically Create a Folder in NetSuite Using SuiteScript?

Learn to automate the creation of new file cabinet folders within NetSuite using modern SuiteScript 2.x APIs.

Short answer

To create a folder in NetSuite via script, use SuiteScript 2.x's `record.create` to instantiate a 'folder' record. Set its `parent` and `name` fields using `record.setValue`. Finally, persist the new folder with `record.save` and log the resulting folder ID for confirmation.

Scenario

NetSuite administrators or developers often need to programmatically manage the file cabinet, including the creation of new folders. Manually creating numerous folders can be time-consuming and prone to error. Automating this process ensures consistency and efficiency, especially for integration or data migration tasks.

Solution

To create a folder programmatically in NetSuite, developers can leverage SuiteScript 2.x. The process involves creating a new folder record, setting its properties such as the parent folder and name, and then saving the record. The following SuiteScript 2.x example demonstrates how to create a root-level folder:

javascript
/**
 * @NApiVersion 2.x
 * @NModuleScope SameAccount
 */
define(['N/record', 'N/log'], function(record, log) {
    function createNewFolderExample() {
        try {
            // Create a new folder record
            var folderRecord = record.create({
                type: record.Type.FOLDER,
                isDynamic: true // Set to true if you need to set values dynamically
            });

            // Set the parent field to create a root-level folder
            folderRecord.setValue({
                fieldId: 'parent',
                value: ''
            });

            // Set the name of the new folder
            folderRecord.setValue({
                fieldId: 'name',
                value: 'Created from SuiteScript'
            });

            // Save the folder record to create it in NetSuite
            var folderId = folderRecord.save();

            // Log the ID of the newly created folder for confirmation
            log.debug({
                title: 'New Folder Created Successfully',
                details: 'New folder ID: ' + folderId
            });

            return folderId; // Return the ID of the new folder

        } catch (e) {
            log.error({
                title: 'Error Creating Folder',
                details: e.message
            });
            return null;
        }
    }
    return {
        createNewFolderExample: createNewFolderExample
    };
});

This script utilizes the N/record module to create and save the folder, and the N/log module to output the new folder's ID for debugging purposes. The parent field is set to an empty string to designate the folder as a top-level folder in the File Cabinet.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant