Currently, we have a SO Order(SO), after clicking the "Drop Ship" button, the page will direct to another page to create a Pruchase Order(PO).
Becasue if the values are grabbed in the pageInit stage, it cost much time to display the record(the more items the more time it costs).
Thus we want to get some values on SO and then put it on PO inside the befordLoad function. But I bump into some trouble for the syntax thus not sure if it is workable for NetSuite.
My script is as follows:
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(['N/record', 'N/ui/serverWidget', 'N/search'],
function (record, serverWidget, search) {
function beforeLoad(context) {
var rec = record.create({
type: record.Type.PURCHASE_ORDER,
isDynamic: true
});
var rec = context.newRecord;
// var createdFromID = rec.getValue({
// fieldId: 'createdfrom'
// });
log.debug('createdFormID', 'test');
log.debug('createdFormID', createdFromID); // 1765886
if (createdFromID != null && createdFromID.length > 0) {
var so_record = record.load({
type: record.Type.SALES_ORDER,
id: createdFromID,
isDynamic: false
});
if (so_record != null) {
//make sure there is a shipping line in the PO, if, create a new line.
// var recordChanged = checkForShippingLine(rec);
log.debug('context.type', context.type);
if (context.type == 'create') {
try {
var po_lines = rec.getLineCount({
sublistId: 'item'
});
var so_costestimate;
for (var poLineNum = 0; poLineNum < po_lines; poLineNum++) {
// var currIndex = rec.selectLine({
// sublistId: 'item',
// line: poLineNum
// });
var soLineID = rec.getSublistValue({
sublistId: 'item',
fieldId: 'custcol_so_line_id',
line: poLineNum
});
log.debug('soLineID', soLineID);
if (soLineID != null && soLineID.length > 0) {
var soLineNumber = so_record.findSublistLineWithValue({
sublistId: 'item',
fieldId: 'custcol_so_line_id',
value: soLineID
});
so_costestimaterate = so_record.getSublistValue({
sublistId: 'item',
fieldId: 'costestimaterate',
line: soLineNumber
});
// rec.setCurrentSublistValue({
rec.setValue({
// sublistId: 'item',
fieldId: 'custcol_so_est_unit_cost',
// line: poLineNum,
// fireSlavingSync: true,
value: parseFloat(so_costestimaterate)
});
rec.commitLine({
sublistId: "item"
});
} //if (soLineNumInPO != null && soLineNumInPO.length > 0)
else {}
} //for(var poLineNum = 1; poLineNum <= po_lines; poLineNum++)
} catch (error) {}
} //if (context.mode == 'create')
}
}
return true;
}
Finally, I found the solution.
Change the rec.setValue{...}
to rec.setCurrentSublistValue{...}
then it works.