Set All Manufacturing Charges to a Custom Unit Price
The following sample updates the prices of all manufacturing charges on the transaction being edited to a unit price of 15.
Note:
This script sample uses the define function, which is required for an entry point script (a script you attach to a script record and deploy). You must use the require function if you want to copy the script into the SuiteScript Debugger and test it. For more information, see SuiteScript 2.x Global Objects.
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/define([
'N/manufacturing/productionCharges',
'N/runtime',
'N/log',
'N/record',
'N/query'
], function(productionCharges, runtime, log, record, query) {
async function afterSubmit(context) {
if (context.type !== context.UserEventType.EDIT) {
return;
}
var newRecord = context.newRecord;
var transactionId = newRecord.id;
// Check API requirements
// Not necessary, but avoids exceptions thrown when calling API
if (runtime.getCurrentUser().getPermission({name: "TRAN_BUILD"}) < runtime.Permission.EDIT) {
log.error({title: 'Permission Error', details: 'Insufficient permissions for Assembly Build'});
return;
}
if (runtime.isFeatureInEffect({feature: "MFGWORKINPROCESS"}) &&
(runtime.getCurrentUser().getPermission({name:"TRAN_WOCOMPL"}) < runtime.Permission.EDIT ||
runtime.getCurrentUser().getPermission({name:"TRAN_WOISSUE"}) < runtime.Permission.EDIT)) {
log.error({
title: 'Permission Error',
details: 'Insufficient permissions for Work Order Completion or Work Order Issue'
});
}
try {
// SuiteQL query to select eligible transaction lines for given transaction
var sql = `
SELECT
tranLine."ID" as id
FROM
TransactionLine tranLine
LEFT OUTER JOIN manufacturingTransaction mt ON tranLine."TRANSACTION" = mt."TRANSACTION"
LEFT OUTER JOIN item invtItem ON tranLine.item = invtItem."ID"
INNER JOIN accountingPeriod accountingPeriod ON mt.postingPeriod = accountingPeriod."ID"
LEFT OUTER JOIN costCategory cc ON invtItem.costCategory = cc."ID"
WHERE
tranLine."TRANSACTION" = ?
AND tranLine.quantity < 0
AND mt.tranType IN ('Build', 'WOCompl', 'WOIssue')
AND invtItem.subtype NOT IN ('Sale')
AND invtItem.itemType IN ('Service', 'OthCharge','NonInvtPart')
AND accountingPeriod.closed = 'F'
AND accountingPeriod.isInactive = 'F'
AND accountingPeriod.isYear = 'F'
AND accountingPeriod.isQuarter = 'F'
AND accountingPeriod.isAdjust = 'F'
AND (cc.itemCostType NOT IN ('OUTSOURCINGCHARGE', 'SERVICE', 'LANDED') OR cc.itemCostType IS NULL)
`;
var results = query.runSuiteQL({query: sql, params: [transactionId]}).asMappedResults();
var transactionLineIds = results.map(function (row) {
return parseInt(row.id, 10);
});
if (transactionLineIds.length === 0) {
log.audit({
title: 'No Eligible Lines',
details: 'No eligible lines found for transaction ID: ' + transactionId + ' (update skipped to avoid throw)'
});
return;
}
var newUnitCost = 15.0;
await productionCharges.updateChargesToCustomUnitCost({
transactionId: transactionId,
transactionLineIds: transactionLineIds,
newUnitCost: newUnitCost
});
log.audit({
title: 'Success: updateChargesToCustomUnitCost',
details: 'Charges updated to ' + newUnitCost + ' for eligible lines ' + transactionLineIds.join(', ') + ' in transaction ID: ' + transactionId
});
} catch (e) {
log.error({
title: 'Error in updateChargesToCustomUnitCost',
details: e.message + ' for transaction ID: ' + transactionId
});
}
}
return {
afterSubmit: afterSubmit
};
});