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

試著忘記壹切 提交于 2021-02-19 03:17:41

问题


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


回答1:


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



回答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.



来源:https://stackoverflow.com/questions/49726167/how-to-upload-a-file-from-local-to-netsuite-file-cabinet-using-suitescript-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!