Create a folder and file using suitescript.

To Create a folder and file in Netsuite’s file cabinet using suitescript.

In this tip i am going to show how to create a folder and file inside Netsuite’s file cabinet.
Nomally we can create a folder and upload files inside the same using Netsuite’s UI. But to do this in suitescript, we have to use certain APIs.

Here the question that arises is that if we can do the above using UI(which is simple and easy), then why in this world do we need to create it using Suitescript…?
The answer to this is that sometimes we create a suitelet which contains a fileupload field and we need to catch that file in POST request and attach that file into Netsuite’s file cabinet inside a certain folder or/and inside that created record from suitelet. Then these APIs come to play.

So i am creating a sample suitelet that contains 3 fields, i.e First Name, Last Name, and File Upload. On submiting the suitelet, an employee record is created and that file is attached to that created employee record.

1. Keeping in mind that we have knowledge in creating a suitelet form, here is a simple form that i have created.

2. In POST request required values are fetched from the suitelet and an employee record is created. The employee id thus retrieved is used to create folder and file. Basically a function is called to create a folder and and insert the file inside the folder (folder name will be employee id).

var file = request.getFile(‘file1’);

var empId = nlapiSubmitRecord(newEmployee);

//— calls CreateFolder() to create a new folder inside specified folder.

var folderId = CreateFolder(file, empId);

3. Previously the root level folder was created in Netsuite under which newly created folders will be located. Here it is set to 10(i.e internal id of root level folder). The folder will be created with the name of the employee’s internal id.

By this what we get as an added advantage is that, we can upload multiple files with same name into the file cabinet, which if done the other way will replace the former.

function CreateFolder(file, empId) {

   try {

      var folder = nlapiCreateRecord('folder');

      if (folder) {

         folder.setFieldValue('parent', '10'); // create root level folder

         folder.setFieldValue('name', empId);

         var folderId = nlapiSubmitRecord(folder);

      }

      //----Creates the file and add it inside the folder created above-----------

      if ((file != null && file != '') && (folderId != null && folderId != '')) {

         var fileCreated = nlapiCreateFile(file.getName(), file.getType(), file.getValue());

         fileCreated.setFolder(folderId);

         var fileId = nlapiSubmitFile(fileCreated);

      }

      //--- calls UpdateRecord() to update the file inside the employee's record.

      if (folderId != null && folderId != '') {

         UpdateRecord(fileId, folderId, empId);

      }

   }

   catch (exception) {

      nlapiLogExecution('DEBUG', 'Error in CreateFolder: ', exception);

   }

return folderId;

}

 

function UpdateRecord(fileId, folderId, empId) {

   try {

      nlapiLogExecution('DEBUG', 'fileid and empId=', fileId + "//" + empId);

      nlapiAttachRecord('file', fileId, 'employee', empId);

   }

   catch (exception) {

      nlapiLogExecution('DEBUG', 'Error in UpdateRecord()', exception);

   }

}

4. Now we can check the created employee record. We shall be able to view the uploaded file inside the record.
Also it will be added inside the filecabinet inside the specified folder.

5. The file cabinet will some what look like this. (“Test Files” is the root folder having internal id 10)

150 150 Burnignorance | Where Minds Meet And Sparks Fly!