netsuite The Scheduled and Map/Reduce Scripts


There are two types of scripts we can leverage for running background processing on a specific, regular interval; these are the Scheduled and the Map/Reduce scripts. Note that the Map/Reduce script type is only available in SuiteScript 2.0. The Scheduled script is available for both 1.0 and 2.0.

The Scheduled script only has a single execute event that gets triggered on whatever schedule you define. For example, you may want to run a nightly script that applies payments to invoices, or an hourly script that syncs data with an external system. When the time interval hits, NetSuite fires this execute event on your Scheduled script.

The Map/Reduce script works similarly, but once it is triggered, it breaks the processing into four distinct phases:

  1. The getInputData phase is where you gather all of the input data you will need to complete the business process. You can use this phase to perform searches, read records, and package your data into a decipherable data structure.
  2. NetSuite automatically passes the results of your getInputData phase to the second phase, called map. This phase is responsible for grouping your input data logically for processing. For instance, if you're applying payments to invoices, you may want to first group the invoices by Customer.
  3. The results of the map phase are then passed to the reduce phase, which is where the actual processing takes place. This is where you would, keeping with our example, actually apply the Payments to the Invoices.
  4. Lastly, a summary phase is invoked that contains data regarding the results of all your processing across the previous three phases. You can use this to generate reports or send out emails that processing is complete.

The major advantage of the Map/Reduce script is that NetSuite will automatically parallelize the processing for you across multiple queues, if available.

Both of these script types have an extremely large governance limit, so you can also use them for bulk processing or generally long-running background processes.

The shortest interval either of these script types can be configured to run is every 15 minutes.

Both of these script types can also be invoked on-demand by users or by other scripts, if necessary.