NetSuite is a powerful cloud-based ERP solution that supports script-based customization through SuiteScript. Among the most versatile tools in a developer’s toolkit are utility scripts—modular, reusable scripts that streamline common operations and improve code maintainability across SuiteScript projects.
This article dives into how to build, customize, and optimize utility scripts in NetSuite, with a focus on practical application.
—
What Are Utility Scripts in NetSuite?
Utility scripts are JavaScript modules or libraries containing functions that can be reused across multiple SuiteScripts (User Event, Suitelet, Scheduled, Map/Reduce, etc.). Their primary role is to encapsulate logic that’s commonly needed, such as:
Date formatting
Record manipulation
Logging
Search creation and execution
Error handling
Response formatting
—
Why Customize Utility Scripts?
Customizing utility scripts provides:
Consistency in logic and formatting across different scripts.
Efficiency by reducing code duplication.
Maintainability through centralized updates.
Scalability for large projects involving multiple developers.
—
Structure of a Utility Script
Utility scripts are typically written as SuiteScript 2.x modules, using AMD-style define blocks. Here’s a basic template:
/**
* @NApiVersion 2.x
* @NModuleScope Public
*/
define([‘N/search’, ‘N/log’, ‘N/record’], function(search, log, record) {
function formatDate(dateObj) {
return dateObj.toISOString().split(‘T’)[0];
}
function logError(title, error) {
log.error({
title: title,
details: error
});
}
function getRecordById(type, id) {
try {
return record.load({
type: type,
id: id
});
} catch (e) {
logError(‘getRecordById failed’, e);
return null;
}
}
return {
formatDate: formatDate,
logError: logError,
getRecordById: getRecordById
};
});
—
Common Custom Utility Modules
Here are some frequently used categories of utility scripts in real-world projects:
1. Search Utilities
Simplify saved search execution and result parsing:
function runSavedSearch(searchId) {
var mySearch = search.load({ id: searchId });
return mySearch.run().getRange({ start: 0, end: 1000 });
}
2. Date Utilities
Standardize date formatting or comparisons:
function isDateInRange(date, start, end) {
return date >= start && date <= end;
}
3. Logging & Error Handling
Centralize error logging:
function handleError(title, e) {
log.error({ title: title, details: e.message });
}
—
Best Practices for Utility Script Customization
Keep utilities atomic: Each function should do one thing well.
Use meaningful names for clarity and maintainability.
Avoid hardcoding internal IDs; pass them as parameters.
Add JSDoc comments to describe each utility function.
Scope appropriately (@NModuleScope Public if shared across scripts).
Version control your utility libraries using Git or another SCM.
—
Real Use Case: Suitelet Invoice Tool
In a Suitelet script for managing invoices, you could use utility scripts for:
Formatting invoice dates
Fetching customer records
Logging API responses
Validating mandatory fields before submission
Example usage in Suitelet:
var util = require(‘./utility_lib.js’);
var invoiceDate = util.formatDate(new Date());
var customer = util.getRecordById(‘customer’, customerId);