Automating invoice creation can streamline billing operations and reduce manual entry errors—especially when dealing with inventory items that require detailed tracking like serial or lot numbers. SuiteScript provides the flexibility to create such records programmatically, including the ability to assign inventory details directly within item lines.
In this article, we demonstrate how to use a Scheduled Script in SuiteScript 2.x to create an Invoice with inventory detail assignments. This solution is particularly useful for companies that use serialized or lot-numbered inventory items and need to issue invoices based on specific inventory numbers. We'll walk through how to construct the invoice, populate the item line, and assign inventory numbers via the inventorydetail subrecord.
Sample Code
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/search', 'N/record'], function(search, record) {
function execute(scriptContext) {
// 1. Create Invoice Record
var recordObject = record.create({
type: record.Type.INVOICE,
isDynamic: true
});
// 2. Set Customer and Location
recordObject.setValue({ fieldId: 'entity', value: 7 }); // Replace 7 with actual customer ID
recordObject.setValue({ fieldId: 'location', value: 1 }); // Replace 1 with actual location ID
// 3. Add Item Line
recordObject.selectNewLine({ sublistId: 'item' });
recordObject.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: 58 }); // Replace 58 with actual item ID
recordObject.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 1 });
recordObject.setCurrentSublistValue({ sublistId: 'item', fieldId: 'description', value: 'Created from script' });
recordObject.setCurrentSublistValue({ sublistId: 'item', fieldId: 'location', value: 1 });
recordObject.setCurrentSublistValue({ sublistId: 'item', fieldId: 'amount', value: 800 });
// 4. Assign Inventory Detail
var subrecordInvDetail = recordObject.getCurrentSublistSubrecord({
sublistId: 'item',
fieldId: 'inventorydetail'
});
subrecordInvDetail.selectNewLine({ sublistId: 'inventoryassignment' });
subrecordInvDetail.setCurrentSublistValue({
sublistId: 'inventoryassignment',
fieldId: 'issueinventorynumber',
value: 19 // Replace with valid Inventory Number internal ID
});
subrecordInvDetail.commitLine({ sublistId: 'inventoryassignment' });
// 5. Commit Item Line and Save Invoice
recordObject.commitLine({ sublistId: 'item' });
recordObject.save({
enableSourcing: false,
ignoreMandatoryFields: true
});
}
return {
execute: execute
};
});
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