Module 6: Scripting Sublists

Week 4 • NetSuite SuiteScript 2.0 Training • ~75 minutes

🎯 Learning Objectives

1. What Are Sublists?

A sublist is a list of related records displayed as a table within a parent record. For example, a Sales Order has an "Items" sublist containing all the line items being sold.

💡 Key Concept

Unlike body fields which have single values, sublists contain multiple lines (rows), each with multiple fields (columns). Think of them as embedded spreadsheets.

Common Sublists

Record TypeSublist IDContains
Sales OrderitemLine items
InvoiceitemBillable items
Purchase Orderitem, expenseItems and expenses
CustomeraddressbookAddresses
EmployeerolesAssigned roles

2. Sublist Elements

Every sublist has three key elements you need to know:

  1. Sublist ID - The internal name of the sublist (e.g., item)
  2. Field ID - The column name (e.g., quantity, rate)
  3. Line Number - Zero-based index (first line = 0)
// To access a value, you need all three:
record.getSublistValue({
    sublistId: 'item',      // Which sublist
    fieldId: 'quantity',    // Which column
    line: 0                 // Which row (0-based)
});
⚠️ Finding IDs

Use the SuiteScript Records Browser to find sublist and field IDs. Go to Help Center → SuiteScript 2.0 → Records Browser. The Records Browser shows all sublists and their fields for each record type.

3. Reading Sublist Values

Get Line Count

var lineCount = record.getLineCount({ sublistId: 'item' });
log.debug('Number of lines', lineCount);

Get Value from Specific Line

// Get quantity from first line
var qty = record.getSublistValue({
    sublistId: 'item',
    fieldId: 'quantity',
    line: 0
});

// Get text display (for list fields)
var itemName = record.getSublistText({
    sublistId: 'item',
    fieldId: 'item',
    line: 0
});

Loop Through All Lines

var lineCount = record.getLineCount({ sublistId: 'item' });

for (var i = 0; i < lineCount; i++) {
    var itemId = record.getSublistValue({
        sublistId: 'item',
        fieldId: 'item',
        line: i
    });
    var qty = record.getSublistValue({
        sublistId: 'item',
        fieldId: 'quantity',
        line: i
    });
    var rate = record.getSublistValue({
        sublistId: 'item',
        fieldId: 'rate',
        line: i
    });
    
    log.debug('Line ' + i, 'Item: ' + itemId + ', Qty: ' + qty + ', Rate: ' + rate);
}

4. Writing to Sublists (Standard Mode)

Set Value on Existing Line

record.setSublistValue({
    sublistId: 'item',
    fieldId: 'quantity',
    line: 0,
    value: 10
});

Insert a New Line

// Insert at specific position
record.insertLine({
    sublistId: 'item',
    line: 0  // Insert at beginning
});

// Set values on the new line
record.setSublistValue({
    sublistId: 'item',
    fieldId: 'item',
    line: 0,
    value: 123  // Item internal ID
});
record.setSublistValue({
    sublistId: 'item',
    fieldId: 'quantity',
    line: 0,
    value: 5
});

Remove a Line

record.removeLine({
    sublistId: 'item',
    line: 2  // Remove third line
});

5. Dynamic Mode Operations

In dynamic mode, you work with sublists like a user would in the UI:

// Load record in dynamic mode
var salesOrder = record.load({
    type: record.Type.SALES_ORDER,
    id: 123,
    isDynamic: true
});

// Select a new line
salesOrder.selectNewLine({ sublistId: 'item' });

// Set values on current line
salesOrder.setCurrentSublistValue({
    sublistId: 'item',
    fieldId: 'item',
    value: 456
});
salesOrder.setCurrentSublistValue({
    sublistId: 'item',
    fieldId: 'quantity',
    value: 10
});

// Commit the line (like clicking Add)
salesOrder.commitLine({ sublistId: 'item' });

salesOrder.save();

Dynamic Mode Methods

MethodPurpose
selectLineSelect existing line for editing
selectNewLineStart adding a new line
setCurrentSublistValueSet value on selected line
getCurrentSublistValueGet value from selected line
commitLineSave the current line
cancelLineDiscard changes to current line

✅ Standard Mode (Server Scripts)

  • Faster performance
  • Direct line access by index
  • setSublistValue()
  • Best for bulk operations

✅ Dynamic Mode (Client Scripts)

  • Triggers field sourcing
  • Select → Edit → Commit workflow
  • setCurrentSublistValue()
  • Mimics user behavior

🏋️ Practice Exercises

Exercise 1: Calculate Line Totals

Create a User Event script that loops through all item lines on a Sales Order and logs each line's amount (quantity × rate).

Exercise 2: Add a Line

Create a script that adds a "Shipping" item to every Sales Order on beforeSubmit.

Exercise 3: Validate Quantities

Create a Client Script validateLine function that prevents committing a line with quantity greater than 100.

🎯 Key Takeaways

  • Sublists need three identifiers: sublistId, fieldId, and line number
  • Line numbers are zero-based (first line = 0)
  • Use getLineCount() before looping through lines
  • Standard mode: setSublistValue() with line number
  • Dynamic mode: selectLine() → setCurrentSublistValue() → commitLine()
  • Use Records Browser to find sublist and field IDs