Transforming Sales Orders into Invoices is a standard NetSuite process—but there are cases where admins need more control before the Invoice is saved, such as updating line memos or quantities automatically.
This article shows how a Scheduled Script can transform Pending Billing Sales Orders into Invoices and update each invoice line with a dynamic memo before saving.
As a NetSuite Administrator, you may want to:
- Automatically Invoice Sales Orders in Pending Billing status
- Apply a consistent memo to each Invoice line (e.g., audit or automation tracking)
- Reduce manual effort and ensure standardized billing notes.
Script Example
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/search', 'N/log', 'N/format', 'N/runtime'], function(record, search, log, format, runtime) {
function execute(context) {
// Get today's date in NetSuite format
var today = format.format({value: new Date(), type: format.Type.DATE});
// 1. Search for Sales Orders Pending Billing
var salesOrderSearch = search.create({
type: search.Type.SALES_ORDER,
filters: [['status', 'anyof', 'SalesOrd:F']], // Pending Billing orders only
columns: ['internalid']
});
var processed = 0; // Counter for processed orders
// 2. Run the search and process each Sales Order found
salesOrderSearch.run().each(function(result) {
var salesOrderId = result.getValue({ name: 'internalid' });
try {
// 3. Transform this Sales Order into an Invoice
var invoiceRecord = record.transform({
fromType: record.Type.SALES_ORDER,
fromId: salesOrderId,
toType: record.Type.INVOICE,
isDynamic: true
});
// 4. For each item line, set/update the memo field
var lineCount = invoiceRecord.getLineCount({sublistId: 'item'});
for (var i = 0; i < lineCount; i++) {
invoiceRecord.selectLine({sublistId: 'item', line: i});
invoiceRecord.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'custcol_line_memo', // Use the correct field ID for your environment
value: 'Automated Invoice: ' + today
});
invoiceRecord.commitLine({sublistId: 'item'});
}
// 5. Save the Invoice and log the result
var invoiceId = invoiceRecord.save();
log.audit('Invoice Created', 'Sales Order ID: ' + salesOrderId + ' → Invoice ID: ' + invoiceId);
processed++;
} catch (e) {
// Log errors but continue processing the next Sales Order
log.error('Transformation Error',
'Sales Order ID: ' + salesOrderId +
' | Err: ' + e.name + ' - ' + e.message
);
}
// 6. Governance check—stop if we’re running low on script usage units
if (runtime.getCurrentScript().getRemainingUsage() < 200) {
log.audit('Governance', 'Stopping to avoid usage limits. Processed: ' + processed);
return false; // break out of the loop
}
return true; // continue to next result
});
// 7. Log a summary when the script completes
log.audit('Script Complete', 'Total Invoices Created: ' + processed);
}
return { execute: execute };
});
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to automate your transactions using SuiteScript? Feel free to share them here!