【问题标题】:Suitescript 2.0 get the contents of a file from a Suitelet file field and use the data in a scheduled scriptSuitescript 2.0 从 Suitelet 文件字段中获取文件内容并在预定脚本中使用数据
【发布时间】:2021-04-20 18:59:30
【问题描述】:

我在工作订单记录上创建了一个按钮,该按钮打开一个带有 fieldType.FILE 的套件表单,以允许用户在本地选择一个文件。提交 Suitlet 后,我​​希望脚本获取文件、获取内容,然后将两个数组传递给预定脚本。到目前为止,我没有任何错误,并且我的 log.debugs 都没有出现在 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
    };


});

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

/**
 * @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)Work Orders 上的UserEventScript -> (2)Work Orders 上的ClientScript -> (3)首先附加 Suitelet -> (4)Suitelet 的ClientScript -> (5) Suitelet 创建任务运行预定脚本 -> (6)ScheduledScript 更新工单。希望获得一些关于从第 3 步到第 5 步获取文件内容的建议,以便在将数据传递到预定脚本之前对其进行解析。

提前致谢

【问题讨论】:

    标签: netsuite suitescript suitescript2.0


    【解决方案1】:

    我认为您可以通过以下方式将文件内容从您的第一个 Suitelet 获取到您的计划脚本:

    首先,您需要将第一个和第二个 Suitelet 合并为一个 Suitelet。这是获取选定文件所必需的。 您可以在您的第一个 Suitelet > 函数 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,您可以在 Web 浏览器的控制台中使用 console.log 而不是 log.debug 查看它的日志(使用 Ctrl 打开+Shift+i)。但是在你的 CS 中获取 'custpage_wo_export' 的值只会返回一个像 'C:\fakepath\your_file_name.file_extension' 这样的字符串。您可能需要先将选定的文件保存在 NetSuite 的文件柜中才能访问其内容。这可以在您的第一个 Suitelet 中的函数 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() 中,现在在第一个 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.
                */
            }
    
        }
    }
    

    注意:在文件柜中选择和上传/保存相同的文件只会覆盖现有文件,保持文件 ID 相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-10
      • 2010-11-02
      相关资源
      最近更新 更多