On-Demand Map/Reduce Parameters

As per my usual post, this is another tricky little gotcha (or should I say gotMe). My problem: A foreground task that rebuilt Item Pricing for a selected customer was timing out. I needed to break it up into chunks and run it in the background as a map/reduce script. In order to do that I had to pass the customer’s ID from a foreground Suitelet to a background map/reduce script.

Note to self: This is super easy, but not super intuitive.

This is a side note. Feel free to skip it.

Why 2 scripts? You probably already know the answer to this or you wouldn’t be reading this post. So “Let me ‘splain. No, there is too much. Let me sum up.” (Inigo Montoya – Princess Bride)

There are different governance rules for foreground and background tasks in NetSuite. Tasks that timeout in the foreground are apportioned more CPU cycles in the background. Map/Reduce is one type of background script that also allows a developer to break up a task into smaller chunks, where each chunk gets its own scope and allotment of CPU cycles. A correctly coded map/reduce script will not time out.

Back to the problem at hand… Drum roll, please! All parameters must be defined with the Script. Below is the Parameters tab under my map/reduce script.

Parameter Definition

It is not intuitive that you need to define parameters here. Keep reading.

Here is the code that runs in the foreground which launches the background map/reduce script. When you read NetSuite’s docs, they show code samples just like this. There is no mention that the parameter must already be defined in the script.

Calling Code Sample

For completeness, here is the code in the map/reduce script which reads the parameter.

Reading the Parameter

If you don’t define the parameter in the called script, all the code works, but the parameter yields a value of NULL in the map/reduce code.

Hopefully, this saves you (and me) time when dealing with passing parameters in the future. Happy coding!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s