In a map/reduce I've got a file load in getInputDa...
# suitescript
k
In a map/reduce I've got a file load in getInputData and the client's existing script does all kinds of data operations over and over again on the same data. I want to just do it once in getInputData and not have to do it again in map and reduce since it's the same values resulting every time. I'd like to get all accounts from the csv, map them, and save them as an array of account mapping object, then same for department and location TL/DR:How would you set a value for a global variable in a map/Reduce based on input data (mapping input csv values to their netsuite equivalents) ANSWERED: Create a "Script Cache" custom record with one long-text field to store the data, then pass that "Script Cache" record to the script as a parameter in the script deployment.
m
I would use the
N/cache
module to hold the result of the repetitive logic.
Copy code
function getAccountMapFromCache() {
    const accountCache = cache.getCache({
        name: 'ACCOUNT',
        scope: cache.Scope.PRIVATE
    });
    
    // returns the value from cache.  If it's not in the cache, calls the loader function
    // and stores the return value in the cache, then returns that value.
    return accountCache.get({
        key: 'account_map',
        loader: getAccountMap,
    });
}

function getAccountMap() {
    // Perform your repetitive logic here and return the value.
}

const accountMap = getAccountMapFromCache();
m
Or pre-chew the file to add the proper accounts/departments/etc to the lines before passing it to map.
k
yeah I started out with the "pre-chew" approach but there are too many lines to process fully in getInputData. I need to just pass the result, unprocessed.
so I'm thinking step 1, in getInputData get an array of all the account, department, and location mappings needed for the entire file (I can iterate through it once I think without killing it) and cache the array, then recall it in map.
So I've created a script cache custom record, add a new record named after my deployment, added it as a parameter to my script, then on my deployment select the cache for this deployment.
the only field is a longtext named cache
Works flawlessly. 😄 I bet this would be handy for passing large data objects to suitelets and tasks as well.
a
You can set parameters with 'N/runtime' and read them across different stages, the parameters values are only persistent during the execution of the Map Reduce.
👍 1
With this you avoid using a custom record or 'N/cache' module.