SuiteScript – Item Groups

Programming Item Groups is not well documented. If you plan to do anything with them via SuiteScript, I’d encourage you to review these notes first.

What brings me to this post? I’m currently augmenting NetSuite’s estimate module. In order to accomplish this, I needed a full working knowledge of Item Groups. Here are my notes, mostly for me, but feel free to use them too.

The Basics:

  1. When you add an item group as a single part, NetSuite expands that to include all parts/items included defined in the group.
  2. Once added to a transaction, the items in the group are fully editable (unlike Kits).
  3. Items are added in the quantities defined in the group and at the current price assigned to a customer. This will include the customer’s “Pricing Group” price. If that is not present, then an alternate price level for that customer. And if that is not present, then the item’s base price. So, standard pricing rules apply!
  4. Under the hood of a transaction, Item Groups will include the following lines:
    1. An item Group definition, complete with item name and description. No price.
    2. A line item for each item included in the group’s definition.
    3. An “EndGroup” item, with an internalId of zero.
  5. To delete an item group, you delete the “Group” item. All sub-items will be removed with it.
  6. To add a group item via SuiteScript, it works differently between server-side and client-side. This is very (very, very) important! At the time of this writing, you won’t find this documented anywhere else.

Here are some examples in SuiteScript 2.0. I’m testing in version 2019.2.

When deleting an item group, simply delete the item with type=”Group”. That takes all items between it and including the item with type=”EndGroup”.

Delete Example

If you are executing a server-side script, in a Suitelet or Restlet, adding an Item Group is as simple as adding an item. Just add the group item and the rest will magically follow.

Server-Side Example

The really tricky scenario is when you’re writing a client-side script. In this case you need to know some rules.

  1. You must add the “Item Group” item first.
  2. Then you must add items that will be included in the group. Since Item groups are editable via a client-side script, you can literally add whatever sub-items you want here! However, it might be nice to actually lookup the items in the group and add those.
  3. If you specify a quantity on any sub-items that differ from the quantities defined in the group, it overrides Item Group defined quantities. This automatically changes the total price of the group. When I say overrides, I mean in the transaction you’re currently working on. The actual definition of the Item Group remains unchanged.
  4. If you specify a rate (unit price) or amount (extended price) that differs from the values defined in the group, it override those values and changes the total on the group (again, in the current transaction).
  5. Quantities, rates and amounts are optional on sub-items. Leave them off, you get system-defined values.
  6. You must add a trailing item of type “EndGroup” with an internalId of zero. It must also have a tax code defined (or at least in my system it did).

Here is an example showing how to add an item group in client-side SuiteScript. In my example, my group actually contains 3 sub-items. I’m only adding one. I’m also changing the quantity, rate (unit price) and amount (extended price) on the sub-item. I was verifying that it correctly updated the group’s total price and cost. It did!

Client-Side Example

I plan to return to this post whenever I’m working on Item Groups. There are simply too many rules to remember. I’ll need these notes!

Here is the code in format you can cut and paste into your debugger.

require([‘N/record’],
    function (record) {
        var rec = record.load({
            type: record.Type.ESTIMATE,
            id: 933121,                     //Demo Estimate with other line items
            isDynamic: true,
        });
        // In this example line 5 is the item with type=”Group”
        // below it are 3 inventory items
        // below them is 1 item of type=”EndGroup”
        rec.removeLine({
            sublistId: ‘item’,
            line: 5,
            ignoreRecalc: true,
        });
        // that last statement just whacked them all!
        rec.save()
    }
)
require([‘N/record’],
    function (record) {
        var rec = record.load({
            type: record.Type.ESTIMATE,
            id: 933121,                     //Demo Estimate with other line items
            isDynamic: true,
        });
        rec.selectNewLine({
            sublistId: ‘item’,
        });
        rec.setCurrentSublistValue({
            sublistId: ‘item’,
            fieldId: ‘item’,
            value: 157309                   // Group Item that includes 3 other items
        });
        rec.setCurrentSublistValue({
            sublistId: ‘item’,
            fieldId: ‘quantity’,
        });
        rec.commitLine({
            sublistId: ‘item’
        });
        var id = rec.save()
    }
)
require([‘N/record’],
    function (record) {
        var rec = record.load({
            type: record.Type.ESTIMATE,
            id: 933121,                     //Demo Estimate with other line items
            isDynamic: true,
        });
        // Group Item
        rec.selectNewLine({ sublistId: ‘item’ });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘item’, value: 157309 });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘groupsetup’, value: true });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘itemtype’, value: ‘Group’ });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, value: 1 });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘taxcode’, value: 38538 });
        rec.commitLine({ sublistId: ‘item’ });
        rec.selectNewLine({ sublistId: ‘item’ });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘item’, value: 52505 });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘ingroup’, value: true });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, value: 5 }); // Override quantity
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘rate’, value: 1000 });   // Override Unit Price
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘amount’, value: 5000 });  // Override Ext. price
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘taxcode’, value: 38538 });
        rec.commitLine({ sublistId: ‘item’ });
        // rec.selectNewLine({ sublistId: ‘item’ });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘item’, value: 52381 });
        // // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘ingroup’, value: true });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, value: 1 });
        // // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘taxcode’, value: 38538 });
        // rec.commitLine({ sublistId: ‘item’ });
        // rec.selectNewLine({ sublistId: ‘item’ });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘item’, value: 52148 });
        // // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘ingroup’, value: true });
        // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘quantity’, value: 1 });
        // // rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘taxcode’, value: 38538 });
        // rec.commitLine({ sublistId: ‘item’ });
        // End Group
        rec.selectNewLine({ sublistId: ‘item’ });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘item’, value: 0 });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘itemtype’, value: ‘EndGroup’ });
        rec.setCurrentSublistValue({ sublistId: ‘item’, fieldId: ‘taxcode’, value: 38538 });
        rec.commitLine({ sublistId: ‘item’ });
        var id = rec.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