4

How can I upload my files to NetSuite file cabinet using SuiteScript 2.0?

amit biswas
  • 75
  • 1
  • 3
  • 7

2 Answers2

10

A possible option is to create a Suitelet with a Document field and save the file uploaded to that field. Here's the code for such a Suitelet:

/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(['N/ui/serverWidget'],
    function(serverWidget) {
        function onRequest(context) {
            if (context.request.method === 'GET') {
                var form = serverWidget.createForm({
                    title: 'Simple Form'
                });

                var field = form.addField({
                    id: 'custpage_file',
                    type: 'file',
                    label: 'Document'
                });

                form.addSubmitButton({
                    label: 'Submit Button'
                });

                context.response.writePage(form);
            } else {
              var fileObj = context.request.files.custpage_file;
              fileObj.folder = 4601; //replace with own folder ID
              var id = fileObj.save();
            }
    }

    return {
        onRequest: onRequest
    };
});
Maria Berinde-Tampanariu
  • 1,011
  • 1
  • 12
  • 25
2

I see you're talking about uploading files from local storage. You can create a file of specified type, based on the record you're working with in your script. By specifying a folder, this file can be saved, while also being available for further processing within your script.

I believe the below can be adapted to use your uploaded file by specifying the 'contents' as 'context.request.files.custpage_file' - as done in Maria's answer.

var exportFolder = '1254';
var recordAsJSON = JSON.stringify(scriptContext.newRecord);

var fileObj = file.create({ 
    name: scriptContext.newRecord.id + '.json',
    fileType: file.Type.JSON,
    contents: recordAsJSON, description: 'My file', encoding: file.Encoding.UTF8,
    folder: exportFolder,
    isOnline: false 
});
var fileId = fileObj.save();

fileObj.save() will return the internal ID of the newly created file in the file cabinet.

Charl
  • 812
  • 1
  • 8
  • 22