In many businesses, generating invoices manually can be time-consuming, especially when billing occurs on a recurring basis or when a large volume of invoices needs to be created regularly. To help streamline this process, NetSuite Scheduled Scripts can automate invoice creation — saving time, reducing errors, and ensuring consistency.
This tip will walk you through a basic Scheduled Script that automatically creates an Invoice for a specific customer with predefined item details.You can customize this further based on your company’s needs, such as dynamically selecting customers, adding multiple items, or setting custom invoice dates.
Here's a basic example:
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record'], function(search, record) {
function execute(scriptContext) {
// Create a new Invoice record
var invoice = record.create({
type: record.Type.INVOICE,
isDynamic: true
});
// Set the customer (Entity ID 7)
invoice.setValue({
fieldId: 'entity',
value: 7
});
// Set the location (Location ID 1)
invoice.setValue({
fieldId: 'location',
value: 1
});
// Add an item line (Item ID 6)
invoice.selectNewLine({
sublistId: 'item'
});
invoice.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'item',
value: 6
});
invoice.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: 1
});
invoice.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'description',
value: 'Created from script'
});
invoice.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'location',
value: 1
});
invoice.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount',
value: 800
});
invoice.commitLine({
sublistId: 'item'
});
// Save the Invoice
invoice.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
}
return {
execute: execute
};
});
- @NApiVersion 2.x: Tells NetSuite you're using the 2.x SuiteScript API (modular format).
- @NScriptType ScheduledScript: Declares that this script is a Scheduled Script, meaning it runs on a scheduled basis (like daily, weekly, etc.).
- N/search module: (Imported but not used here) Usually helps find records.
- N/record module: Used to create, load, edit, and save records — in this case, an Invoice.
var invoice = record.create({
type: record.Type.INVOICE,
isDynamic: true
});
- Creates an Invoice in Dynamic Mode, meaning you can set values interactively and immediately affect the record (just like manually editing a form).
invoice.setValue({ fieldId: 'entity', value: 7 });
invoice.setValue({ fieldId: 'location', value: 1 });
- entity: The Customer for the invoice (Internal ID 7).
- location: The Location to associate the invoice with (Internal ID 1).
invoice.selectNewLine({ sublistId: 'item' });
invoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: 6 });
invoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 1 });
invoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'description', value: 'Created from script' });
invoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'location', value: 1 });
invoice.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: 800 });
invoice.commitLine({ sublistId: 'item' });
- selectNewLine: Start adding a new line under the Items sublist.
- item, quantity, description, location, amount: Sets each field's value for the item being billed.
- commitLine: Finalizes and saves the current item line entry.
invoice.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
- enableSourcing: false: Field sourcing (auto-filling values) is disabled.
- ignoreMandatoryFields: true: Ignores missing mandatory fields — useful during early development or if you're controlling field values manually.
For more information on handling scripts, refer to the following:
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.
How are you using Scheduled Script in your business? Share them in the NetSuite Admin Corner