Automating record creation becomes more efficient with the SuiteScript 2.1 Promise API. This modern scripting approach allows you to handle asynchronous operations with cleaner syntax and better performance, helping administrators and developers build faster, more maintainable scripts.
In this example, you will learn how to programmatically create a Sales Order with multiple items for a recurring customer. By leveraging the Promise API, you can improve performance, streamline logic, and ensure more reliable error handling.
Sample Script: Create Sales Order with Multiple Items
/**
* @NApiVersion 2 .1
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/log'], (record, log) => {
const execute = async (context) => {
// Replace with real customer and item IDs in your instance
const customerId = 1234; // Existing Customer Internal ID
const item1Id = 567; // First Inventory Item Internal ID
const item2Id = 890; // Second Inventory Item Internal ID
try {
// Create Sales Order asynchronously
const salesOrder = await record.create.promise({
type: record.Type.SALES_ORDER,
isDynamic: true
});
// Set required header fields
salesOrder.setValue({ fieldId: 'entity', value: customerId });
salesOrder.setValue({ fieldId: 'memo', value: 'Order created via SuiteScript 2.1 Promise API' });
// Add first item
salesOrder.selectNewLine({ sublistId: 'item' });
salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: item1Id });
salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 2 });
salesOrder.commitLine({ sublistId: 'item' });
// Add second item
salesOrder.selectNewLine({ sublistId: 'item' });
salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'item', value: item2Id });
salesOrder.setCurrentSublistValue({ sublistId: 'item', fieldId: 'quantity', value: 1 });
salesOrder.commitLine({ sublistId: 'item' });
// Save sales order asynchronously
const salesOrderId = await salesOrder.save.promise();
// Log success
log.audit({
title: 'Sales Order Created',
details: `Sales Order ID: ${salesOrderId}`
});
} catch (error) {
log.error({
title: 'Error Creating Sales Order',
details: error
});
}
};
return { execute };
});
- Always validate your IDs before deploying in production.
- Use try/catch for safe error management.
- Keep your scripts maintainable—prefer async/await when working with multiple asynchronous operations.
- Always develop and test in sandbox environments first.
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!