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 ofrun()to process large datasets efficiently in batches. - Avoid unnecessary field retrievals; use
search.lookupFields()instead ofrecord.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 ofrecord.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 ofrecord.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.