2

Run into a kind of silly problem.

I want to pass a variable between stages on a map/reduce script. Is it there an "official" or best way to do this (rather than sending it with the returned results).

This is my last approach:

    /**
    * @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

felipechang
  • 916
  • 6
  • 14

2 Answers2

7

NetSuite stores whatever you return from the previous phase in context.value.

No matter what data type you return, it will always get sent to the next phase as a String, so you need to JSON.parse it if you want to work with a different data type.

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

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

You cannot get access to specific variables from previous phases. If you want to pass data along, you need to build an appropriate data structure with the values you want to pass and return it, then parse it out of context.value in the subsequent phase.

erictgrubaugh
  • 8,519
  • 1
  • 20
  • 28
  • 1
    Yeah I'm not sure I like the fact you need to parse it yourself; I feel like NetSuite should take care of the type conversion for you. – erictgrubaugh Aug 17 '16 at 19:26
  • It's probably for security. I feel a lot of what has been done on SS2.0 was just made to 'feel' like modern JS, but the backend is pretty much the same. – felipechang Aug 18 '16 at 18:42
  • Yes, still Rhino-based server-side infrastructure. Bring on node! – erictgrubaugh Sep 08 '16 at 14:08
1

This is coming a little late, but one solution/workaround I found is using a Scheduled Script to call a Map/Reduce Script.

You can dynamically set script parameters within the Scheduled Script when you initiate the map/reduce script (using the 'N/task' module).

Chris Lacaille
  • 137
  • 1
  • 5