Sublist Updates – Performance Issues

After some testing, I found that when updating estimate line items, you’ll want to bulk your requests. Loading and saving the estimates is very timeconsuming. So load the estimate, make all your updates to line items and then save the estimate. This becomes a problem if you are removing a subset of items. Here’s how I accomplished the task. 

  1. My example is in SuiteScript 2.0
  2. I’m removing 3 non-consecutive lines (Lines 1, 2 & 4).
  3. In my example, the lines I wish to remove are not supplied in order.
  4. You must do this in Dynamic mode. If you are unfamiliar with the 2 modes in SuiteScript, standard and dynamic, dynamic mode loads all the sublist items when you load the main (estimate) record.
  5. I’m removing Estimate Lines. Each line gets a line number. All sublists work the same.
  6. SuiteScript 2.0 references line numbers starting with a zero-based index. Note: This differs from SuiteScript 1.0 which is one-based.
  7. DON’T MISS THIS. Loading an estimate (in my environment) takes 4 seconds. Saving the estimates adds another 4 seconds. So I want to load the estimate, remove lines, and then save the updated estimate. This eliminates lots of overhead!!!
  8. When you remove a line, it changes the subsequent line numbers. So remove items from bottom to top in the list! You can ignore this if you are removing all lines. In that case, you can simply loop through all items, repeatedly removing line 0 (or 1 in SuiteScript 1).
  9. I’ve included the save() function, written for both client-side and server-side. Client-side is commented out.
  10. Finally, this example is meant for debugger. It uses require() instead of define().

Here is the code.

Code Example

Here is the code in a form you can cut and paste into debugger.

require([‘N/record’, ‘N/log’],
    function (record, log) {
        // These are the line numbers that we will be deleting ()
        var linesToDelete = [1, 4, 2]; // Notice the order
        // Sort this array in decending order
        sortedLinesToDelete = linesToDelete.sort(function (a, b) { return a < b ? 1 : -1 });
        var currentQuote = record.load({
            type: record.Type.ESTIMATE,
            id: ‘[your transaction internalid goes here]’,
            isDynamic: true
        });
        for (var i = 0; i < sortedLinesToDelete.length; i++) {
            currentQuote.removeLine({
                sublistId: ‘item’,
                line: (sortedLinesToDelete[i] – 1), // SuiteScript 2.0 uses a zero based index!
                ignoreRecalc: true,
            });
        }
        // Server side… no promise. It’s synchronous.
        currentQuote.save();
        log.debug(linesToDelete.length + ‘ items removed’);
        // // Client side, you may want the promise
        // currentQuote.save.promise()
        //     .then(
        //         function () {
        //             log.debug(linesToDelete.length + ‘ items removed’);
        //         }
        //     )
    }
)

 

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 )

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