SuiteScript – Find Sublist Line by Value

This one eluded me. I needed to update Item Pricing records under a customer. So how to avoid iterating through every item in the sublist in order to find the line to update? Here’s the secret!

  • Load the customer using isDynamic = false (Standard Mode)
  • Lookup the line number of the line you need update using record.findSublistLineWithValue()
  • Then simply update the line just like you always do using the line number (zero based offset).

Here is a code example.

Here is the code in a format that you can cut and paste.

var customer = record.load({
    type: record.Type.CUSTOMER,
    id: kvp.customer_id,
    isDynamic: false
})

updated_kvp.pricing_items.forEach(
    function (pricing_item) {
        if (pricing_item.hasOwnProperty('unit_price')) {

            var lineNumber = customer.findSublistLineWithValue({
                sublistId: 'itempricing',
                fieldId: 'item',
                value: pricing_item.id
            });

            if (lineNumber >= 0) {
                customer.setSublistValue({
                    sublistId: 'itempricing',
                    fieldId: 'price',
                    line: lineNumber,
                    value: pricing_item.unit_price
                });
            }
        }
    }
)

customer.save();

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s