ANS-1152 · SUITESCRIPT DEVELOPMENT

How to Dynamically Remove Sublist Line Items in NetSuite Client Scripts

Learn how to effectively clear custom sublist entries using SuiteScript 2.x event triggers like PostSourcing.

Short answer

To reliably remove sublist line items in NetSuite client scripts, implement the logic within the PostSourcing event. While fieldChanged may not consistently trigger line removal, PostSourcing ensures proper execution for custom sublists. Utilize SuiteScript 2.x functions like currentRecord.getLineCount() and currentRecord.removeLine() for current best practices.

Scenario

Users attempting to dynamically remove line items from a custom sublist in NetSuite client scripts may encounter issues where the removal logic fails to execute consistently, particularly when triggered by a fieldChanged event. The desired behavior is to clear all existing lines from a specific custom sublist when a related field, such as 'customer', is modified.

Solution

To ensure successful removal of sublist line items, particularly from custom sublists, the logic should be placed within the PostSourcing client script event. While attempts to use the fieldChanged event for this purpose may not yield consistent results, PostSourcing reliably executes the necessary operations. The PostSourcing event is triggered after a field's value has been sourced, making it an ideal point to adjust related sublist data. The following SuiteScript 2.x example demonstrates how to clear all lines from a custom sublist with the ID custpage_rebate_discount when the 'customer' field is sourced or changed:

javascript
/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 */
define(['N/currentRecord'], function(currentRecord) {
    function postSourcing(context) {
        var rec = context.currentRecord;
        var fieldName = context.fieldId;

        if (fieldName'customer')
        {
            while (rec.getLineCount({ sublistId: 'custpage_rebate_discount' }) > 0)
            {
                rec.removeLine({
                    sublistId: 'custpage_rebate_discount',
                    line: 0
                });
            }
        }
    }
    return {
        postSourcing: postSourcing
    };
});

This script leverages currentRecord.getLineCount() to determine the number of lines and currentRecord.removeLine() to iteratively remove them by always targeting the first line (index 0) until the sublist is empty.

Expert NetSuite Support

Need help with this NetSuite issue?

SuiteScript Development consulting and configuration support

Talk to a consultant