Suitescript 2.0 从 Suitelet 文件字段中获取文件内容并在预定脚本中使用数据

Suitescript 2.0 get the contents of a file from a Suitelet file field and use the data in a scheduled script

我在工单记录上创建了一个按钮,用于打开带有 fieldType.FILE 的套件表单,以允许用户在本地选择文件。提交 Suitlet 时,我希望脚本获取文件、获取内容,然后将两个数组传递给预定脚本。到目前为止,我没有任何错误,我的 log.debugs 中的 none 出现在 Netsuite 中。

这里是套件代码供参考:

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * 
 * @author MF
 */

define(['N/ui/serverWidget', 'N/file', 'N/search', 'N/email','N/ui/dialog'], function (serverWidget, file, search, email, dialog) {
    function onRequest(context) {
        //getWoData(context);
        buildPage(context);
    };
    function buildPage(context) {

        var woForm = serverWidget.createForm({
            title: "Mass Update Work Order Due Dates",
        })
        woForm.addField({
            id: 'custpage_wo_export',
            label: "Work Order Update File",
            type: serverWidget.FieldType.FILE
        });
        woForm.addSubmitButton({
            id: 'custpage_submitwo',
            label: 'Submit',
            functionName: 'SubmitWorkOrders'
        });
        woForm.clientScriptModulePath = "SuiteScripts/WoSubmitClient.js"
        context.response.writePage(woForm);
    }

return {
        onRequest: onRequest
    };
});

据我所知,问题始于客户端。 这是上面 Suitelet 的客户端脚本:

/**
 * @NApiVersion 2.X
 * @NScriptType ClientScript
 * @NModuleScope SameAccount
 * @author MF
 */
define(['N/url', 'N/https','N/currentRecord'], function (url, https, currentRecord) {
    function pageInit(context) { };

    function SubmitWorkOrders(context) {
       var objRecord = currentRecord.get();
        //Handle the save button
        log.debug({
            title: 'SubmitWorkOrders',
            details: "This function was called"
        })
        var uploadedFile = objRecord.getValue({
            fieldId: 'custpage_wo_export'
        })
        log.debug({
            title: 'File',
            details: uploadedFile.name
        })
        var taskCreate = url.resolveScript({
            scriptId: 'customscript_scheduledscripttaskagent',
            deploymentId: 'customdeploy_scheduledscripttaskagent',
            returnExternalUrl: false,
            params: {
                input_file: uploadedFile
            }

        });
    }


    return {
        pageInit: pageInit,
        saveRecord: SubmitWorkOrders
    };


});

此客户端脚本然后调用另一个 suitelet 来创建和 运行 任务。

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * 
 * @author MF
 */

define([
    'N/task',
    'N/redirect',
    'N/file'
], function (task, redirect, file) {
    function scriptTask(context) {

        //check file type
        var input_file = context.request.params.input_file;
        log.debug({
            title: 'File Name',
            details: input_file.name
        })
        var woUpdateScriptTask = task.create({
            taskType: task.taskType.SCHEDULED_SCRIPT,
            deploymentId: 'customscript_womassupdate',
            params: { uploadedFile: uploadedFile },
            scriptId: 715
        }).submit();
    }
    return {
        onRequest: scriptTask
    };
});

如有任何建议,我将不胜感激,我进入 Netsuite 领域才几周时间,javascript 通常只比这长一点。请注意,其中一些脚本中有一些来自测试不同方法的残留模块。

目前整个过程看起来像这样 -> (1)UserEventScript on Work Orders -> (2)ClientScript on Work Orders -> (3)First attached Suitelet -> (4)ClientScript for Suitelet -> ( 5)Suitelet 创建任务到运行 scheduled script -> (6)ScheduledScript 更新工单。希望获得有关从第 3 步到第 5 步获取文件内容的一些建议,以便可以在将数据传递给预定脚本之前对其进行解析。

提前致谢

我认为这是从第一个 Suitelet 获取文件内容到预定脚本的方法:

首先,您需要将第一个和第二个 Suitelet 合并为一个 Suitelet。这是获取选定文件所必需的。 您可以在第一个 Suitelet > function onRequest():

中简单地这样做
function onRequest(context) {
    if (request.method == 'GET') { //This block will execute when you open your Suitelet form
        buildPage(context);
    }else{ //This block will execute when Submit button is clicked
        scriptTask(context); // add scriptTask() function body in your 1st Suitelet from 2nd Suitelet
    }
};

其次,如果您的客户端脚本附加了 Suitelet,您可以使用 console.log 而不是 log.debug[ 查看它的日志=27=] 在 Web 浏览器的控制台中(使用 Ctrl+Shift+i 打开)。但是在您的 CS 中获取 'custpage_wo_export' 的值只会 return 一个像 'C:\fakepath\your_file_name.file_extension' 这样的字符串。您可能需要先将所选文件保存在 NetSuite 的文件柜中才能访问其内容。这可以在您的第一个套件中的函数 scriptTask() 中完成。

在客户端脚本 > 函数 SubmitWorkOrders() 中,使用:

function SubmitWorkOrders(context) {
    var objRecord = context.currentRecord;
    var uploadedFile = objRecord.getValue({
        fieldId: 'custpage_wo_export'
    })
    console.log('File', uploadedFile);

    return true; //use return 'false' to view the log in console first
}

现在第三,在函数 scriptTask() 中,现在在 1st Suitelet 中,使用:

function scriptTask(context) {
    if (context.request.files.custpage_wo_export) {
        var fileObj = context.request.files.custpage_wo_export; //save this fileObj and then access it's contents.
        log.debug('fileObj', fileObj);
        fileObj.folder = 1630; //speficy the id of folder in File Cabinet
        var fileId = fileObj.save();
        log.debug("fileId", fileId);

        //Now, load newly saved file and access it's contents. You can also access file contents in your Scheduled script depending on what you prefer/require.
        if (fileId) {
            var fileObj = file.load({
                id: fileId
            });
            fileContents = fileObj.getContents();
            log.debug("fileContents", fileContents);

            /*
                Now, you have fileId and/or fileContents. You can execute your Scheduled script here using task.create() etc.
            */
        }

    }
}

注意:在文件柜中选择和uploading/saving相同的文件将简单地覆盖现有文件,保持文件 ID 相同。