求助:补全NetSuite SuiteScript销售订单创建脚本的else分支
typeof Value == 'object' Branch in Your SuiteScript Sales Order Script Hey there! Since you’ve got solid JavaScript basics under your belt, let’s break this down and fill in that missing branch for your SuiteScript sales order creation script. First, a quick recap: your existing code validates the record type, creates a NetSuite record, and loops through incoming data to set fields. The gap is handling cases where a value is an object—this is almost certainly for sublists (like the items sublist on a sales order, which requires adding rows and setting fields per row).
Here’s the full, completed function with the missing branch filled in:
function getItems(datain) { var err = new Object(); // Validate if mandatory record type is set in the request if (!datain.recordtype) { err.status = "failed"; err.message = "missing recordtype"; return err; } var record = nlapiCreateRecord(datain.recordtype); // Iterate over all keys in the incoming data for (var fieldName in datain) { if (datain.hasOwnProperty(fieldName)) { var value = datain[fieldName]; // Skip the recordtype since we already used it to create the record if (fieldName === 'recordtype') continue; // Handle regular fields (non-object values like strings/numbers) if (typeof value !== 'object' || value === null) { record.setFieldValue(fieldName, value); } // Handle sublists (object/array values) else { // Most common case: sublist data is an array of row objects if (Array.isArray(value)) { value.forEach(function(rowData, rowIndex) { // Add a new blank row to the sublist record.selectNewLineItem(fieldName); // Set each field for this sublist row for (var subField in rowData) { if (rowData.hasOwnProperty(subField)) { record.setCurrentLineItemValue(fieldName, subField, rowData[subField]); } } // Save the row to the sublist record.commitLineItem(fieldName); }); } // Edge case: single sublist row passed as a plain object else { record.selectNewLineItem(fieldName); for (var subField in value) { if (value.hasOwnProperty(subField)) { record.setCurrentLineItemValue(fieldName, subField, value[subField]); } } record.commitLineItem(fieldName); } } } } // Try saving the record and return success/error details try { var recordId = nlapiSubmitRecord(record); return { status: "success", recordId: recordId, message: "Record created successfully" }; } catch (e) { err.status = "failed"; err.message = e.message || "Error creating record"; return err; } }
Key Details for the Missing Branch:
- Sublist Workflow: In SuiteScript 1.0, populating a sublist (like the
itemlist on a sales order) follows three critical steps:selectNewLineItem(sublistId): Creates a blank new row in the sublistsetCurrentLineItemValue(sublistId, fieldId, value): Sets individual fields on the active rowcommitLineItem(sublistId): Saves the row to the sublist
- Flexible Data Handling: The code works for both arrays of sublist rows (the standard use case) and single-row objects (a less common but possible edge case)
- Safety Checks: We skip the
recordtypefield (since we already used it to create the record) and usehasOwnPropertyto avoid iterating over inherited object properties - Robust Error Handling: Added a try/catch around
nlapiSubmitRecordto capture and return any errors that pop up when saving the record
Example Usage for a Sales Order:
If you pass in data like this, the sublist branch will automatically populate the order’s items:
var salesOrderData = { recordtype: 'salesorder', entity: '123', // Customer's internal ID in NetSuite trandate: '2024-05-20', item: [ { item: '456', // Item's internal ID quantity: 2, rate: 50.00 }, { item: '789', quantity: 1, rate: 100.00 } ] }; var result = getItems(salesOrderData); console.log(result);
This should work smoothly for creating sales orders (or any other NetSuite record with sublists) since it handles both regular fields and sublist data correctly.
内容的提问来源于stack exchange,提问作者tkansara

