什么是在SS2.0中传递变量的“官方”方式?

时间:2016-08-17 14:33:12

标签: netsuite suitescript

遇到一种愚蠢的问题。

我想在map / reduce脚本的各个阶段之间传递一个变量。是否存在“官方”或最佳方式(而不是使用返回的结果发送)。

这是我的最后一种方法:

    /**
    * @NApiVersion 2.0
    * @NScriptType MapReduceScript
    */

    define(["N/search", "N/record", "N/email", "N/runtime", "N/task", "/SuiteScripts/Libraries/tools_lib"],
    function (search, record, email, runtime, task, tools) {

    var ss = runtime.getCurrentSession();
    var conf = {};

    /**
     * Retrieve the CSV contents and return as an object
     * @return {Array|*}
     */
    function getInputData() {

        log.debug("setting", "foo");
        ss.set({name: "foo", value: "bar"});

        //Session
        var foo = ss.get({name: "foo"});
        log.debug("foo 1", foo);

        //var pass
        conf["foo"] = "bar";

        return [1, 2, 3];
    }

    /**
     * Search and group by type, all records with matching entries on the CSV field
     * @param context
     * @return {boolean}
     */
    function map(context) {

        //Session
        var foo = ss.get({name: "foo"});
        log.debug("foo 2", foo);

        //Var pass
        log.debug("foo 3", conf["foo"]);

        return false;
    }

foo 1 = bar

foo2 = null

foo3 = null

2 个答案:

答案 0 :(得分:5)

NetSuite会存储return中上一阶段context.value的任何内容。

无论您返回什么数据类型,它都会始终以String的形式发送到下一个阶段,因此如果您想使用其他数据类型,则需要JSON.parse。< / p>

function getInputData() {
  return [1,2,3];
}

function map(context) {
  log.debug(JSON.parse(context.value));
}

您无法访问之前阶段的特定变量。如果要传递数据,则需要使用要传递的值和return构建适当的数据结构,然后在后续阶段将其解析出context.value

答案 1 :(得分:1)

这有点晚了,但是我发现一个解决方案/解决方法是使用计划脚本来调用Map / Reduce脚本。

启动地图/缩小脚本时,可以在“计划脚本”中动态设置脚本参数(使用“ N /任务”模块)。