amit biswas
amit biswas

Reputation: 75

How to upload a file from local to NetSuite file cabinet using SuiteScript 2.0

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

Upvotes: 4

Views: 8574

Answers (2)

Charl
Charl

Reputation: 827

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.

Upvotes: 2

Maria Berinde-Tampanariu
Maria Berinde-Tampanariu

Reputation: 1041

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
    };
});

Upvotes: 10

Related Questions