Background
While subject to a separate article, during a client implementation of a consignment sales flow, we sought to couple the specific sale of an item to the creation of a related vendor bill. In this case, since the items were serialized, we knew exactly where we acquired the item by looking it up via its related item receipt.
The goal thus was to constitute a vendor bill and bind it to the purchase order. However, we did not seek to use the SuiteScript “record.transform” option due to the way that the sales transactions could reference more than one item across different respective purchase orders.
SuiteScript Pattern to Link Vendor Bill Lines to Purchases Orders
The approach uses the built-in orderdoc and orderline references in the vendor bill item sublist.
Here is the code pattern:
billRec.selectLine ( {sublistId: 'item', line: i } );
billRec.setCurrentSublistValue (
{
sublistId: 'item',
fieldId: 'orderdoc',
value: poReference
}
);
billRec.setCurrentSublistValue (
{
sublistId: 'item',
fieldId: 'orderline',
value: poLineId
}
);
billRec.commitLine ( {sublistId:'item'} );
There are a couple of considerations to account for:
- On Create: you can only link on the creation of the vendor bill, not on its update. However, we did not test if we could update the vendor bill and add newly created lines to bind to the purchase order.
- Orderline Index: To get the orderline index, you will need some lookup code. Else you may receive an error, “You cannot add an item and purchase order combination that does not exist to a Vendor Bill.”
Call the following function to get the PO Line:
var poLine = PO.findSublistLineWithValue({sublistId: "item", fieldId: "item", value: data.itemId});
Then get the Orderline value using the PO Line as an index pointer:
var orderLine = PO.getSublistValue({sublistId: "item", fieldId: "line", line: poLine});
Note, the term “line” is ambiguous because it both exists as a field and is also used to reference the array index.