In a map reduce script is it possible to run a one...
# suitescript
m
In a map reduce script is it possible to run a one time search and set a value to a constant for later use in processing? I'd like to do something like :
Copy code
function getInputData(context){
     var strArrayValue = ["A", "B", "C"]; // these are the product of a netsuite search that I only want to run once
     return someOtherResultSetToIterateOver;
}

function map(context){
     // on each of the resultset records set the field to the values in the constant
     var itemRec = record.load({               // Loading Record
                type:"inventoryitem",
                id:context.id,
                isDynamic: true
                 )};

     itemRec.setText('custbody_multiselectfield', strArrayValue); // Setting Value (Array List) for Multi-Select Fields
     itemRec.save();    // Saving Loaded Record
}
I don't see params being passed in the return section of any examples or else I would just use that.
b
the usual workaround is to use N/cache
m
ooooo I am new to that module, I'll check it out thank you
it doesn't seem like there's a good way to utilize the cache to store the resultset of a search
that's a bummer
b
turn the results into string key/value pairs
m
so, I set the const at the top of the define, run the search in the getInputData, save the kvp to cache, and when I call the cache again in map I just extrapolate the data using like JSON.parse?
b
depends on your data
m
simple internal ids of the subsidaries on the account
very limited but effective criteria
b
if was literally ['A', 'B', 'C'] you could also just store it as "A,B,C" and split on the comma to change it into an array
m
I get you, I'm looking to set the subsidiary field for incoming items during an integration, and this search will determine the default value of that field during the create/update functions
so more like ['1','2','3','4']
b
be careful of scope in map/reduce scripts
m
meaning the cache could give out and not have a value?
b
it used to be that a new script scope was created for each iteration of a map/reduce script
m
0_0 it's not anymore?
so what happens on yield?
b
now the same script scope is reused until the map/reduce yields
when it yields, everything is evaluated again
m
oh /sigh I expected that, I thought you were saying it didn't reset on yield, damn near gave me a heart attack
b
do not rely on sharing a cache variable between functions
m
ok sounds like cache isn;t the way to go then
b
the cache lives outside of the script scope
you just need to get it again
m
I can hardcode the value for now, or force the script to search for it over and over 😕 I was hoping for a more elegant solution but idk what else to do
so if I put the search in the getInput data it would get it again wouldn't it?
b
the data in the cache is persistant
you can get it multiple times (even in different scripts) without putting data in the cache again
you may want to experiment with the cache first
m
I guess maybe I'm not understanding map/reduce then. Does each run of the script call each function? or is that not how it works?
b
it does
but not necessarily in the same context
context here having the meaning javascript environment
m
okay, I don't expect the result set to change but it seems like you're cautioning me about scope which means maybe the const would not have a value due to ... governance running out and the value being technically blank because it couldn't be set during that getinputdata call but meanwhile the map function is still going on some other run where it is then setting subsidiary to blank when it shouldn't
... am I tracking?
seems risky then
b
if you did
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */
define(["N/cache"], function (cache) {
  var x;

  function getInputData() {
    x = cache.getCache({ name: "battksCache" });
    return new Array(10000000);
  }

  function map(context) {
    log.debug("x", typeof x);
  }

  return {
    getInputData: getInputData,
    map: map,
  };
});
x may not exist by the time you get to map
if your map/reduce yields in between calls to map, the javascript environment will be lost
m
I feel like that's the most important part of this
b
when it starts up again, a new environment will be created which will not do getInputData again
m
anytime I can't trust the cache to have the value it breaks what I'm trying to do
b
you need to use getCache in the map to get the cache again
m
I can do that, should I force map to exit if the value doesn't exist?
b
what value?
m
using your example x
b
getting x is a mistake
dont do it
call getCache again in the map to get the cache again
m
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */
define(["N/cache"], function (cache) {
  var x;
  function getInputData() {
      return new Array(10000000);
  }
  function map(context) {
        x = cache.getCache({ name: "battksCache" });
      log.debug("x", typeof x);
  }
  return {
    getInputData: getInputData,
    map: map,
  };
});
like that?
b
its not wrong, but i wouldnt make the x variable in the map function
m
thanks for your help @battk
b
whoops
m
...?
b
i typed that wrong, i mean i would make the x variable in the map function
Copy code
/**
 * @NApiVersion 2.0
 * @NScriptType MapReduceScript
 */
define(["N/cache"], function (cache) {

  function getInputData() {
      return new Array(10000000);
  }
  function map(context) {
       var x = cache.getCache({ name: "battksCache" });
      log.debug("x", typeof x);
  }
  return {
    getInputData: getInputData,
    map: map,
  };
});
m
lol so more like var x ... instead of just x =
yeah