如何在NetSuite中为已有销售订单添加代发货采购订单
I’ve run into this exact scenario before—NetSuite’s UI makes it look simple with that "Drop Ship" link, but the API requires using specific methods instead of just setting field values. Let’s break down why your current code isn’t working and how to fix it.
Why Your Current Approach Fails
The fields you’re trying to set (createpo, povendor, createdpo) are UI-only markers or read-only system fields, not triggers for NetSuite’s backend logic:
createpojust tells the UI to show the Drop Ship option, but doesn’t initiate PO generation.createdpois a system-managed field that NetSuite populates automatically when a PO is linked; manually setting it won’t establish the proper relationship between the SO line and PO.
The Correct Solution: Use NetSuite’s Built-in Drop Ship PO API
NetSuite provides dedicated APIs to generate Drop Ship POs programmatically, which handle all the behind-the-scenes association logic between the sales order line and the new PO. Here’s how to implement it for both SuiteScript versions:
SuiteScript 2.x
Use the record.createDropShipPO() method. This method creates the PO and automatically links it to the specified sales order line.
/** * @NApiVersion 2.x * @NScriptType UserEventScript */ define(['N/record'], function(record) { function createNewDropShipPO(soId, lineNumber, vendorId) { try { // Create the Drop Ship PO and link it to the SO line var poId = record.createDropShipPO({ salesOrderId: soId, line: lineNumber, // Note: Line numbers start at 0 for SuiteScript 2.x vendor: vendorId }); log.debug('Drop Ship PO Created', 'PO ID: ' + poId); return poId; } catch (e) { log.error('Error Creating Drop Ship PO', e.message); throw e; } } // Example usage (e.g., in a user event or scheduled script) // createNewDropShipPO(12345, 0, 67890); return { // Hook this to your desired script event (e.g., beforeSubmit, afterSubmit) }; });
SuiteScript 1.0
Use the nlapiCreateDropShipPO() function, which works similarly:
/** * @NApiVersion 1.x * @NScriptType UserEventScript */ function createNewDropShipPO(soId, lineNumber, vendorId) { try { // Line numbers start at 1 for SuiteScript 1.x var poId = nlapiCreateDropShipPO(soId, lineNumber, vendorId); nlapiLogExecution('DEBUG', 'Drop Ship PO Created', 'PO ID: ' + poId); return poId; } catch (e) { nlapiLogExecution('ERROR', 'Error Creating Drop Ship PO', e.message); throw e; } } // Example usage // createNewDropShipPO(12345, 1, 67890);
Key Notes & Limitations
- Sales Order State: Even though you can’t revert to "Pending Approval", the SO should still be in a state that allows modifications (e.g., "Pending Fulfillment", "Partially Fulfilled"). If the SO is fully closed, you won’t be able to generate a new PO.
- Item Configuration: Ensure the item on the SO line has the Drop Ship option enabled in its item record.
- Multiple POs per Line: NetSuite allows multiple Drop Ship POs for a single SO line only if the original PO wasn’t fully received. If the first PO was fully received, you may need to adjust the SO line quantity first (if allowed) before generating a new PO.
内容的提问来源于stack exchange,提问作者Michael McCauley

