Unexpected SuiteScript Error - Creating Vendor Bills from Purchase Orders

What happened

We were recently tasked with helping a company automatically generate vendor bills each day for purchase orders that were received during the day. During the test phase of the project, we noted that sometimes the bill save action would fail with an “Unexpected SuiteScript Error.”

The weird thing is that the script would work sometimes, but fail other times. Once a bill failed for a particular vendor/group of purchase orders, it would always fail; other bills for the same vendor would process without error.

We couldn’t detect a pattern, so we swallowed our pride and submitted a case to NetSuite for assistance. We got on a zoom call with two support engineers yesterday, and with their assistance, we were able to pinpoint the issue.

Linking a Vendor Bill to Multiple Purchase Orders

Marty Zigman has discussed this topic on his blog in detail, so readers may already be familiar with the technique.

In short, to link a vendor bill to a purchase order, you use the orderdoc and orderline fields on the item sublist of the vendor bill to link that bill back to the original purchase order.

By doing this, you can link the vendor bill to multiple purchase order and purchase order lines.

Our issue

When we spoke with support, we were able to pinpoint the issue to the orderdoc field. Somehow between querying the list of purchase orders to get their internal IDs and setting the orderdoc field with one of the purchase order IDs, NetSuite saw the purchase order ID as numeric float value, so instead of internal ID "12345" it was trying to put "12345.0" in the orderdoc field.

The fix

The fix for the error is quite simple. Just prior to setting the orderdoc field, we parse the input as an integer, and convert the integer to a string like this:

const orderDoc = String(parseInt(purchaseOrderId, 10))
vendorBillRec.setCurrentSublistValue({
    fieldId: 'orderdoc',
    value: orderDoc, // Now converted to a string/integer
    sublistId: 'item',
})

I’m hopeful that I’ll never run into this error again, but if I ever get an “Unexpected SuiteScript Error” again while building a script, this fix will be one of the things I apply to any internal IDs.