Sample scheduled script to update Price level in the Invoice record to custom and update rate based on the requirement.
/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
*/
define([‘N/log’, ‘N/record’],
/**
* @param{log} log
* @param{record} record
*/
(log, record) => {
/**
* Defines the Scheduled script trigger point.
* @param {Object} scriptContext
* @param {string} scriptContext.type – Script execution context. Use values from the scriptContext.InvocationType enum.
* @since 2015.2
*/
const execute = (scriptContext) => {
let invoiceId = ‘48640’; // Provide the invoice Id that is required to be updated.
try {
let invoiceRecord = record.load({
type: record.Type.INVOICE,
id: invoiceId,
isDynamic: true
});
let itemLineCount = invoiceRecord.getLineCount({ sublistId: ‘item’ });
for (let i = 0; i < itemLineCount; i++) {
invoiceRecord.setSublistValue({
sublistId: ‘item’,
fieldId: ‘price’,
line: i,
value: -1 // set custom value
});
// Set the unit price to 0.80
invoiceRecord.setSublistValue({
sublistId: ‘item’,
fieldId: ‘rate’,
line: i,
value: 0.80
});
log.debug(‘Updated Line’, ‘Line ‘ + i + ‘ updated with Unit Price: 0.80’);
}
invoiceRecord.save({
enableSourcing: true,
ignoreMandatoryFields: true
});
log.debug(‘Invoice Saved’, ‘Invoice ‘ + invoiceId + ‘ updated successfully’);
} catch (e) {
log.error(‘Error Processing Invoice’, ‘Invoice ID: ‘ + invoiceId + ‘ Error: ‘ + e.message);
}
}
return { execute }
});