Update the Last Manufacturing Charge to the Current Purchase Price
The following sample updates the price of the last manufacturing charge on the transaction being edited to the current purchase price.
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/query'
], function(productionCharges, runtime, log, query) {
async function afterSubmit(context) {
if (context.type !== context.UserEventType.EDIT) {
return;
}
var newRecord = context.newRecord;
var transactionId = newRecord.id;
// Basic permission check
// Not strictly necessary, but avoids exception 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;
}
// MFGWORKINPROCESS permission check - same as permission check above
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;
}
transactionLineIds = transactionLineIds.sort((a, b) => a - b);
transactionLineIds =
transactionLineIds.slice(transactionLineIds.length - 1,transactionLineIds.length); // Only update last eligible line
// Call the method asynchronously
await productionCharges.updateChargesToItemPurchasePrice({
transactionId: transactionId,
transactionLineIds: transactionLineIds
});
log.audit({
title: 'Success: updateChargesToItemPurchasePrice',
details: 'Charges updated for eligible lines ' + transactionLineIds.join(', ') + ' in transaction ID: ' + transactionId
});
} catch (e) {
log.error({
title: 'Error in updateChargesToItemPurchasePrice',
details: e.message + ' for transaction ID: ' + transactionId
});
}
}
return {
afterSubmit: afterSubmit
};
});