For Map/Reduce scripts in 2.0, is it possible go b...
# suitescript
t
For Map/Reduce scripts in 2.0, is it possible go back and forth between records in the map stage? For example, let’s say I want to read a value from a field on one record and then copy that value to a record that I already iterated through in the map stage?
OR in addition to the above, is there a way to retain a value in a variable through the entire map stage?
e
Build your object in getInputData stage to make it available across all the other stages.
t
@Eric B would I pass that object from getInputData along with my savedSearch data to the map stage as a parameter?
Ex: getInputData() { …… return savedSearch, valueObject } map: function(context, valueObject) { …. } Would this work?
e
I’m not sure that would work since each stage only takes in context as a parameter.
t
@Eric B hmmm… do you think I could write it to the context as a specific name? And that way I could access it later?
s
generally speaking, it's not what Map/Reduce was meant for. Each instance of Map is meant to be processed independent of all other results. Reduce can process multiple records, provided they all share a common key. What is the exact use case? Is there some kind of relationship between the reference record and the current record? If so, you may be able to build a saved search or SuiteQL query that relates them somehow.
e
I usually build an object from my saved search results so that I can add object properties to that object that can be passed to the other stages.
t
The ultimate goal is to be able to read a value off of one expense record and copy that value to another debit record in order to balance a journal entry for our company. The issue is that I can read the value off the reference record but when I want to copy it over to another record it is lost because the map f(x) executes anew for another record. I’m not able to retain any values across the entirety of the map stage
e
It might help to share your code…
e
Sounds like you could be using the reduce stage if you're not already. Do your copy from the source (context.key) to the target (context.values)
n
You can pass an array of JSON objects that contains all the data you need you just need to structure it something like
[
{searchresult:{},staticvalues:{}},
{searchresult:{},staticvalues:{}}
]
then the data for each map iteration would have the info you need. So build that static data you need for every record and then iterate your results building out the new array including both pieces of data to be passed forward. In your map you're just going to pull in data.searchresult and data.staticvalues or am I missing something? (this was in response to "OR in addition to the above, is there a way to retain a value in a variable through the entire map stage?")
t
@NElliott @ehcanadian I am working with a saved search that returns both a credit record and a corresponding debit record for some employees (114 total records). I am trying to read through all the records returned from the saved search and assess if there is a non-zero number in a credit record. If so, then I need to access the corresponding debit record for the same employee, copy the credit amount to the debit record. My issue is that I am having trouble seeing how to access a different record in the map stage as I can’t find a way to 1) retain the credit amount in a variable throughout the entire map stage/ entire script execution and 2) access a different debit record when a credit amount in present in a credit record. My question is if this is possible or do I need to switch script types?
e
I am not following why you're not able to access the records in
map
since you're passing the data to it. What data do you need to be persistent across all the map iterations? At any rate, in
map
, you can write to mapContext so it gets grouped and passed to
reduce
. (There is an example in NS help for M/R which relates to invoices. It may shed some light.)
n
@Tim Johnson I'm with @ehcanadian on this, I'm not sure why you would be struggling to persist a value, as long as you include it with every map object you're using you're golden. I'm on UK time so may miss your response but feel free to DM me if you'd like to dig in to this more or maybe share code here to make the issue clearer.
s
@Tim Johnson if there is a common value between the two records (one is linked to the other, or they are both linked to a common transaction, for example), then you can use the Map stage as it was designed, to map both the debit and credit results to the same key (whatever value is common to both). Then, in your Reduce phase, you should get multiples of two values per key. Just be sure that if multiple debits or credits have the same common value, then the Reduce phase will contain more than one credit and debit, but it should still be a much smaller array, and you can iterate through that array multiple times without incurring any governance cost. This is one of the really great features of Map/Reduce scripts, and why there is a phase called Map (you are able to map multiple results to a single key, so they can be processed together in Reduce)