There is a Sales Order having many Purchase Orders, and the Sales Order has an item which sums up all the shipping cost in each PO.
I develop 2 functions as follows to calculate all the shipping cost in each PO:
function calcuTotalShippingCostByAllPOs(.....) {
......
// var created_pos is an array which records all the unique PO id in SO
for (i = 0; i < created_pos.length; i++) {
var po_amountValue = po_record.getSublistValue({
sublistId: 'item',
fieldId: 'amount',
line: ship_line
});
total_shipping += parseFloat(po_amountValue);
}
so_record.setSublistValue({
sublistId: "item",
fieldId: "amount",
line: so_ship_line,
// ignoreFieldChange: false,
value: total_shipping
});
}
function calcuTotalShippingCostByCurrentPO(.....) {
......
switch (context.type) {
case context.UserEventType.CREATE:
var difference = new_po_ship_cost;
break;
case context.UserEventType.DELETE:
var difference = -old_po_ship_cost;
break;
default:
var difference = new_po_ship_cost - old_po_ship_cost;
break;
}
// Get the old GST-Shipping cost on SO
var old_so_ship_cost = so_record.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_po_actual_cost',
line: so_ship_line
});
// if PO UNIT COST on ship line is a finite number
if (isNumber(old_so_ship_cost)) {
var new_so_ship_cost = old_so_ship_cost + difference;
} else {
var new_so_ship_cost = difference;
}
so_record.setSublistValue({
sublistId: "item",
fieldId: "amount",
line: so_ship_line,
// ignoreFieldChange: false,
value: total_shipping
});
}
But if someone updates the shipping cost in a specific PO during the calculation, then the total shipping cost may be not correct.
Is it possible to lock the POs while executing the function to sum up all shipping cost?
For example, if there are 100000 POs in a SO, during the calculation of total shipping cost, if someone modify one of the shipping cost in a PO, then the total cost may be not be correct if the cost has been added in the total shipping cost.
How to prevent data inconsistency from happening on NetSuite.