Best Practices to Handle Governance in a Map/Reduce Script

Handling governance efficiently in a Map/Reduce script ensures smooth execution without hitting NetSuite’s API limits.

1. Get (Load) Input Data Stage

This stage fetches data to process in the script.

Best Practices:

  • Use search.create().runPaged() instead of run() to process large datasets efficiently in batches.
  • Avoid unnecessary field retrievals; use search.lookupFields() instead of record.load() when only a few fields are needed.
  • Limit the number of records fetched; apply filters and criteria in the search to get only necessary data.
  • Optimize file handling; load files only if required and process them in chunks.

2. Map Stage

This stage processes each record individually.

Best Practices:

  • Use search.lookupFields() instead of record.load() whenever possible to reduce governance usage.
  • Process lightweight data transformations in this stage and delegate heavy processing to Reduce.
  • Use caching techniques like storing frequently used values in an object to avoid repetitive API calls.
  • Track governance usage (context.usage) and reschedule the script if nearing the limit.

3.Reduce Stage

This stage consolidates and processes key-value pairs from the Map stage.

Best Practices:

  • Batch process records instead of updating each record separately.
  • Use submitFields() instead of record.submit() when updating a few fields to save governance units.
  • Minimize the number of record.load() calls by passing necessary data from the Map stage.
  • Use rescheduleCurrentScript() if governance usage is high.

4. Summarize Stage

This stage logs results, errors, and performs any final processing.

Best Practices:

  • Store key metrics in a custom record instead of logging everything.
  • Avoid unnecessary updates to records; only perform essential database operations.

Leave a comment

Your email address will not be published. Required fields are marked *