Anyone knows how to do iteration in getInputData s...
# suitescript
v
Anyone knows how to do iteration in getInputData stage before sending to map stage? I need the sample code to better understand
s
You have two different options within getInputData: return a search or query, which NS will run for you, serialize all of the results, and send it directly to the next stage (map or reduce if there is no map). Or, you can run any code you like, and return an array, object, or file. You can build whatever array or object you like and return that, there’s no right way. But, I’d step back a bit. With Map/Reduce, you often do not need to do this. Use the Map phase for what it is intended: grouping results by shared keys, so that in the reduce phase you can handle multiple results at the same time. No need to iterate. For example, if you want to group results by unique combination of subsidiary and transactionperiod, then combine those two values into a unique key in the map phase:
const key = [subsidiary, transactionperiod].join('|');
then write that key and any values needed for the JE as an object at the end of the map function:
context.write(key, valuesObject);
you’ll then have access to an array of values in reduce, grouped by each individual key. you’ll just have to split the key back into its separate values:
const [subsidiary, transactionperiod] = context.key.split('|');
1000 1
👍 1
r
@vennila ramasamy I think you're conceptually confused about some general programming concepts including data and control structures. When people say iterate, it just means some kind of looping or repetition. You run the search and using
run
and
each
or
runPaged
with
forEach
. Something along those lines. This is just basic search processing and I'm sure you've done this. On top of this, from previous discussions it seems you need to spend some time learning doing various things related to
JSON
.
A JavaScript crash course might be a good idea.
r
I usually add the logic to do any sort of data manipulation etc from the search in the input stage itself, gives me more leeway if I wanted to scale in the future for which using another stage (reduce) might be required. But what Scott has suggested will be better at this point. As you will not have to create the JSON. But you should learn how to create / work with objects.
💯 1
r
I would process the results in GID. If you return a search, you'll have to deduce what the object structure is like by spelunking in the object. Which is fine, but may take some time to do if it's your first time doing that. If you process the results in GID, you know what to expect in MAP. I would just commit to using a JSON object. Arrays are fine, but you'll need objects eventually. Remember to
JSON.parse
in MAP.
💯 1
Start by returning something like [{ key: 'key', value: 'value' }] from GID and then figure out how to get the values in MAP.
s
I suppose some of this comes down to preference. I like doing data manipulation/grouping/filtering in the map phase, and consider it an intermediate step between raw data gathering (GID) and processing (reduce). Everyone should do what feels most natural to them, as you will be the one troubleshooting and maintaining the code. It’s far more important that it works and you can understand it, than anything else. When using Map/Reduce you must get comfortable with parsing JSON, as that is how most complex data is passed from one step to the next.
1
r
Ya agreed it's a matter of preference. There are some use cases when it makes sense it break it up into GID > update Item in Map, update BOMs and BOM Revisions in Reduce, but for most cases a Reduce stage is enough to handle pretty much anything, and it's preferable to Map since it has more processing.