Updating Purchase Order quantities can help purchasing teams respond to revised stocking requirements. However, applying the same quantity across multiple items can create unexpected spending increases when item prices vary.
For example, a purchasing team may need to adjust eligible purchase order lines to 10 units, while allowing each proposed change only when the resulting line amount stays within 500 in the transaction currency, excluding tax
Using SuiteScript’s cancelLine() method, an administrator can build this check into an automated update: evaluate the proposed quantity change, keep acceptable edits, and discard edits that exceed the limit.
Sample Script
/**
* @NApiVersion 2 .1
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/log'], (record, log) => {
// Entry point called when the scheduled script runs.
function execute() {
// Replace with the internal ID of your purchase order.
const PURCHASE_ORDER_ID = 12345;
// Quantity to apply to each eligible item line.
const TARGET_QUANTITY = 10;
// Maximum allowed line amount in the transaction currency.
// This example checks the line amount, excluding tax.
const MAX_LINE_AMOUNT = 500;
try {
// Load the purchase order in dynamic mode.
// cancelLine(), selectLine(), and commitLine() require this mode.
const purchaseOrder = record.load({
type: record.Type.PURCHASE_ORDER,
id: PURCHASE_ORDER_ID,
isDynamic: true
});
// Get the number of existing lines on the Items sublist.
const lineCount = purchaseOrder.getLineCount({
sublistId: 'item'
});
// Track edits kept in the loaded record and edits discarded.
let committedLines = 0;
let canceledLines = 0;
// Review each item line. SuiteScript line indexes start at 0.
for (let line = 0; line < lineCount; line++) {
// Reusable helper to read a field on the current loop line.
// Reading with getSublistValue() does not select the line.
const readField = (fieldId) =>
purchaseOrder.getSublistValue({
sublistId: 'item',
fieldId,
line
});
// Read the item type and quantity before making an edit.
const itemType = readField('itemtype');
const originalQuantity = Number(readField('quantity'));
// Limit this example to inventory, non-inventory,
// and service items. Skip other item types.
const supportedItemTypes = [
'InvtPart',
'NonInvtPart',
'Service'
];
// Skip unsupported items, closed lines, and lines that
// already have the target quantity.
if (!supportedItemTypes.includes(itemType) ||
readField('isclosed') === true ||
originalQuantity === TARGET_QUANTITY) {
continue;
}
// Select the existing line so current-line methods can edit it.
purchaseOrder.selectLine({
sublistId: 'item',
line
});
// Set the proposed quantity without committing the edit.
// Dynamic mode sources and recalculates dependent values.
purchaseOrder.setCurrentSublistValue({
sublistId: 'item',
fieldId: 'quantity',
value: TARGET_QUANTITY
});
// Read the recalculated amount from the selected line.
const proposedAmount = Number(
purchaseOrder.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'amount'
})
);
// Reject the edit if the amount is invalid or above the limit.
if (!Number.isFinite(proposedAmount) ||
proposedAmount > MAX_LINE_AMOUNT) {
// Discard the uncommitted edit to the selected item line.
// This preserves the existing line; it does not delete it.
purchaseOrder.cancelLine({
sublistId: 'item'
});
// Count the discarded edit for the execution summary.
canceledLines++;
// Log the rejected proposal using human-readable
// line numbering, which starts at 1.
log.audit({
title: 'Purchase order line edit canceled',
details: {
line: line + 1,
originalQuantity,
proposedQuantity: TARGET_QUANTITY,
proposedAmount,
limit: MAX_LINE_AMOUNT
}
});
// Move to the next line without committing this edit.
continue;
}
// Keep the acceptable edit in the loaded record.
// commitLine() does not save the purchase order to the database.
purchaseOrder.commitLine({
sublistId: 'item'
});
committedLines++;
}
// Save once after processing all lines.
// Avoid saving when no line edits were committed.
if (committedLines > 0) {
purchaseOrder.save();
}
// Record the outcome in the script execution log.
log.audit({
title: 'Purchase order review completed',
details: {
purchaseOrderId: PURCHASE_ORDER_ID,
committedLines,
canceledLines
}
});
} catch (error) {
// Log load, field validation, commit, or save failures.
log.error({
title: 'Purchase order review failed',
details: error
});
// Rethrow so NetSuite reports the execution as failed.
// cancelLine() does not automatically handle these exceptions.
throw error;
}
}
// Expose the scheduled script entry point.
return { execute };
});
DISCLAIMER: 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!