4

How do I create a new folder in SuiteScript 2.0 and save it to the file cabinet?

var folder = record.create({
    type: record.Type.FOLDER,
});

folder.save()    

What am I missing?

cja
  • 9,512
  • 21
  • 75
  • 129
Morris S
  • 2,337
  • 25
  • 30

2 Answers2

10

Here's a snippet which creates a folder using SS2.0:

        var objRecord = record.create({
            type: record.Type.FOLDER,
            isDynamic: true
        });
        objRecord.setValue({
            fieldId: 'name',
            value: 'Test Folder'
        });
        var folderId = objRecord.save({
            enableSourcing: true,
            ignoreMandatoryFields: true
        });
cja
  • 9,512
  • 21
  • 75
  • 129
Maria Berinde-Tampanariu
  • 1,011
  • 1
  • 12
  • 25
2

This is what I use to save a file to a specified folder:

var exportFolder = runtime.getCurrentScript().getParameter({name: 'custscript_export_folder'});
var fileObj = file.create({ 
        name: scriptContext.newRecord.id + '.json',
        fileType: file.Type.JSON,
        contents: recordAsJSON, description: 'Products sent to warehouse', encoding: file.Encoding.UTF8,
        folder: exportFolder,
        isOnline: false 
        });
    var fileId = fileObj.save();

If the specified folder does not exist, it will be created. In my example, I pass the folder name as a parameter. This is simply specified as "Folder1/folder2". If folder1 exists and folder2 does not, folder2 will be created in folder1.

Charl
  • 812
  • 1
  • 8
  • 22
  • According to https://docs.oracle.com/cloud/latest/netsuitecs_gs/NSAPI/NSAPI.pdf, the folder property: "Internal ID of the folder that houses a file within the NetSuite File Cabinet.". Are you sure you made it work with a string path? _I get an error when I try this_ – cja Sep 26 '19 at 15:35
  • This was a working solution of mine from a while back. Unfortunately I don't have access to those projects anymore, so I have no idea if this is still in use and if still working. – Charl Sep 27 '19 at 10:23