问题
I'm trying to get the serial numbers from a Fulfillment record in a Netsuite account that is using Advanced Inventory (unlike the Netsuite regular inventory system, Advanced Inventory stores the serial number in a subrecord to the transaction record). I've tried using the example that Netsuite provides and I get a NULL response. I've tried accessing an existing Saved Search and I get the wrong fields (the Saved Search works fine from the UI). Does anyone know what the trick is? Here's the code example that Netsuite provides -
var ffill=nlapiLoadRecord('itemfulfillment', 5892, {recordmode: 'dynamic'});
ffill.selectLineItem('item', 1);
var invDetailSubrecord = ffill.viewCurrentLineItemSubrecord('item', 'inventorydetail');
invDetailSubrecord.selectLineItem('inventoryassignment', 1);
nlapiLogExecution('DEBUG', 'inventory number: ' + invDetailSubrecord.getCurrentLineItemValue('inventoryassignment', 'receiptinventorynumber'));
With the above code I get NULL from the Debug statement - "Debug Serial Mgr - Create inventory number = null"
And here is the code using an existing Saved Search -
var results = nlapiSearchRecord('itemfulfillment', 620, null, null);
var result = results[0];
var columns = result.getAllColumns();
var columnlen = columns.length;
for (ci = 0; ci < columnLen; ci++)
{
var column = columns[ci];
var label = column.getLabel();
var value = result.getValue(column);
nlapiLogExecution('DEBUG','Serial Mgr', 'Columns ' + label + " " + value);
With this code I get 3 columns from the search but the wrong fields (Itemship and 553 are not correct) - "Debug Serial Mgr Columns null 10/16/2013" "Debug Serial Mgr Columns null ItemShip" "Debug Serial Mgr Columns null 553"
回答1:
Try the following for the label
var label = column.getLabel()||column.getText();
回答2:
Here is how I do it for item fulfillment. This is run inside of a scheduled script as it is potentially "costly" for us as our clients may have very large order fulfillments.
var context = nlapiGetContext();
var internalID = context.getSetting('SCRIPT', 'custscriptitemfulfillment_internalid');
var rec = nlapiLoadRecord("itemfulfillment", internalID);
var count = rec.getLineItemCount("item");
for (var k = 1; k <= count; k++) {
var internalItemID = rec.getLineItemValue("item", "item", k);
var itemRecordType = nlapiLookupField('item', internalItemID, 'recordtype');
if (itemRecordType == 'serializedinventoryitem') {
var subRec = rec.viewLineItemSubrecord('item', 'inventorydetail', k);
if (!(!subRec || subRec == null)) {
var serialNo = [];
var snc = 0;
var subRecIntId = subRec.getFieldValue('id');
var filters = [new nlobjSearchFilter('internalid', null, 'is', subRecIntId)];
var columns = [
new nlobjSearchColumn('inventorynumber', 'inventorynumber'),
new nlobjSearchColumn('quantity'),
new nlobjSearchColumn('binnumber')
];
var arrSearchResults = nlapiSearchRecord('inventorydetail', null, filters, columns);
for (var m = 0; m < arrSearchResults.length ; m++) {
var currSerNo = '';
var src_serNo = arrSearchResults[m].getValue(columns[0]);
var src_qty = Math.abs(arrSearchResults[m].getValue(columns[1]));
var src_bin = arrSearchResults[m].getValue(columns[2]);
if (src_qty > 0) {
if (src_bin && src_bin != null && src_bin > '') {
currSerNo = { serialNumber: src_serNo, quantity: src_qty, binNumber: src_bin };
}
else {
currSerNo = { serialNumber: src_serNo, quantity: src_qty };
}
serialNo[snc] = currSerNo;
snc++;
}
}
// snc has a count of all the serial numbers on that fulfilled item
// serialNo is an array with the serialized items, including their location and bin when applicable.
// These two get reset on the next line item so process them now.
}
}
}
Any errors are due to renamed variables and reducing the code to remove additional functionality.
来源:https://stackoverflow.com/questions/19418994/script-to-get-netsuite-advanced-inventory-serial-numbers