Introduction
SuiteScript gives developers deep control over NetSuite customization โ but without proper optimization, scripts can easily hit governance limits or slow down processing.
This post breaks down proven performance techniques, code examples, and real scenarios that help you write scripts that are faster, cleaner, and scalable.
Why Optimization Matters
Each SuiteScript operation consumes governance units. If your script doesnโt handle them efficiently, youโll face:
SSS_USAGE_LIMIT_EXCEEDEDerrors.- Long processing times.
- Partial or failed data updates.
Optimizing SuiteScripts helps you run large jobs, improve user experience, and scale integrations safely.
๐ง 1. Reduce Record Loads
Every record.load() is expensive. Instead, use search.lookupFields() or record.submitFields() when you only need specific values.
โ Inefficient:
var rec = record.load({ type: record.Type.CUSTOMER, id: id });
var email = rec.getValue('email');
โ Optimized:
var fields = search.lookupFields({
type: search.Type.CUSTOMER,
id: id,
columns: ['email']
});
var email = fields.email;
This avoids loading the entire record.
โ๏ธ 2. Use Batching or Pagination for Searches
Processing all results at once can exceed governance. Always use runPaged() with a sensible page size.
var pagedData = search.load({ id: 'customsearch_sales' }).runPaged({ pageSize: 1000 });
pagedData.pageRanges.forEach(range => {
var page = pagedData.fetch({ index: range.index });
page.data.forEach(result => {
// process
});
});
โ Efficient, scalable, and safe for large datasets.
๐ 3. Use Map/Reduce for Bulk Processing
Map/Reduce scripts are designed for large workloads. They automatically yield, reschedule, and run in parallel queues.
Use them instead of scheduled scripts when processing thousands of records.
define(['N/search', 'N/record'], (search, record) => ({
getInputData: () => search.load({ id: 'customsearch_orders' }),
map: context => {
const data = JSON.parse(context.value);
record.submitFields({
type: data.recordType,
id: data.id,
values: { custbody_processed: true }
});
}
}));
๐ง 4. Minimize Logging in Loops
Logging inside loops adds time and governance usage. Use batch logging or summaries.
if (i % 100 === 0) {
log.audit('Progress', `Processed ${i} records`);
}
โก 5. Cache Reusable Data
When working with reference data (like tax codes or subsidiaries), cache it in memory or use script parameters.
if (!context.cachedSubsidiary) {
context.cachedSubsidiary = search.lookupFields(...);
}
๐งฉ 6. Replace Repeated Searches with Filters
If youโre filtering results dynamically, combine multiple filters instead of creating new searches inside loops.
var mySearch = search.create({
type: 'salesorder',
filters: [['status', 'anyof', 'SalesOrd:A']],
columns: ['internalid']
});
๐ Before vs. After Optimization
| Metric | Before Optimization | After Optimization |
|---|---|---|
| Average Execution Time | 18 min | 5 min |
| Governance Units Used | 8,500 | 2,300 |
| Record Loads | 3,000 | 250 |
| Script Failures | Frequent | None |
๐งฑ 7. Use Asynchronous & Promise Patterns (SuiteScript 2.1)
Combine optimization with async/await for non-blocking calls.
const customer = await record.load.promise({ type: 'customer', id: 101 });
This speeds up API-driven or external integration scripts.
๐งฎ 8. Control Governance Thresholds
Always check remaining usage before proceeding:
if (runtime.getCurrentScript().getRemainingUsage() < 200) {
task.create({ taskType: task.TaskType.SCHEDULED_SCRIPT }).submit();
}
This ensures your script yields safely instead of crashing.
๐งฐ 9. Use Field Lookups for Simple Updates
When updating only one or two fields, never reload the full record.
record.submitFields({
type: record.Type.SALES_ORDER,
id: 1005,
values: { custbody_status: 'Processed' }
});
๐งพ 10. Bundle Reusable Code
Organize frequently used utilities (e.g., logging, rescheduling, search helpers) in a Library Script.
This minimizes duplication and improves maintainability.
Conclusion
SuiteScript performance optimization is not about writing less code โ itโs about writing smarter code.
By reducing record loads, batching searches, caching data, and leveraging Map/Reduce and async patterns, youโll create scripts that are both high-performing and governance-friendly.
These small improvements can save hours of processing time in large-scale integrations and financial processes.
Learn more about NetSuite Scripting Tutorials & different NetSuite Customization resources
Discover more from The NetSuite Pro
Subscribe to get the latest posts sent to your email.
Leave a Reply