Expand your SuiteScript skills in the Sustainable SuiteScript community for NetSuite developers.

6 Inline Editing in SuiteScript 2.0

Created: June 5, 2017

NetSuite's inline editing feature allows users to very quickly modify and update the data for a particular record without having to load the entire record on a page, edit the form, then save the record.

NetSuite developers can also leverage inline editing via SuiteScript's submitFields functionality. This feature is provided by the nlapiSubmitField function in SuiteScript 1.0 and the N/record#submitFields method in SuiteScript 2.0.

N/record#submitFields

As with most of the record functionality we've seen so far, there is a 1:1 relationship between the SuiteScript 1.0 and 2.0 inline editing functionality. The 2.0 submitFields function has the same options provided by the 1.0 nlapiSubmitField function. We can set a single field or multiple fields, and when we do so, we can determine whether sourcing should occur and whether to ignore mandatory fields.

The best way to illustrate the actual usage will be through an example.

Example: Editing a Single Field

Let's start with a simple example in the afterSubmit of a User Event deployed to the Sales Order record. We'll use inline editing to set the default shipping item for the associated Customer.

// 1.0
function afterSubmit(type) {
    var customerId = nlapiGetFieldValue("entity");
    var shippingItem = getDefaultShippingItem();
    if (!(customerId && shippingItem)) {
        return;
    }
    nlapiSubmitField("customer", customerId, "shippingitem", shippingItem);
}

function getDefaultShippingItem() {
    // custom logic for determining the shipping item
}
// 2.0 - Console
/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
require(["N/record"], function (r) {
    function updateShippingItem(context) {
        var customerId = context.newRecord.getValue({fieldId: "entity"});
        var shippingItem = getDefaultShippingItem();
        if (!(customerId && shippingItem)) {
            return;
        }
        r.submitFields({
            type: r.Type.CUSTOMER,
            id: customerId,
            values: {"shippingitem": shippingItem}
        });
    }

    function getDefaultShippingItem() {
        // custom logic for determining the shipping item
    }
    
    return {afterSubmit: updateShippingItem};
});

Example: Editing Multiple Fields

We can also inline-edit multiple fields with a single call; let's update our example to also set a comment on the Customer when we update the shipping item. We'll also look at how to specify the sourcing and mandatory fields options.

// 1.0
function afterSubmit(type) {
    var customerId = nlapiGetFieldValue("entity");
    var shippingItem = getDefaultShippingItem();
    if (!(customerId && shippingItem)) {
        return;
    }
    nlapiSubmitField(
        "customer",
        customerId,
        ["shippingitem", "comments"],
        [shippingItem, "Shipping item modified."],
        true,
        false
    );
}

function getDefaultShippingItem() {
    // custom logic for determining the shipping item
}
// 2.0 - Console
/**
 * @NApiVersion 2.0
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 */
require(["N/record"], function (r) {
    function updateShippingItem(context) {
        var customerId = context.newRecord.getValue({fieldId: "entity"});
        var shippingItem = getDefaultShippingItem();
        if (!(customerId && shippingItem)) {
            return;
        }
        r.submitFields({
            type: r.Type.CUSTOMER,
            id: customerId,
            values: {"shippingitem": shippingItem, "comments": "Shipping Item Modified."},
            options: {enableSourcing: true, ignoreMandatoryFields: false}
        });
    }

    function getDefaultShippingItem() {
        // custom logic for determining the shipping item
    }

    return {afterSubmit: updateShippingItem};
});

Notice in 2.0, we just need to add more key-value pairs to the values Object to update more fields, and we specify the sourcing and mandatory fields settings using the options property.

As handy and effective as inline editing can be, it also has its drawbacks and limitations. Let's take a look at some of the pros and cons of inline editing.

Advantages and Limitations of Inline Editing

The largest advantage of inline editing is that it executes faster and uses less governance than loading the record, modifying the fields, and saving the record. This works on both standard and custom records. It also involves significantly less code, which is always nice.

Unfortunately, not all fields are inline-editable.

Sublists are not inline-editable; if you need to work with sublist fields, you will need to load the record.

Further, select fields (dropdowns) and subrecord fields (like addresses) are not inline-editable either. To work with these fields, you'll need to load the record.

Finally, only very specific fields on each record type are inline-editable; to determine which ones are inline-editable, you need to check the " nlapiSubmitField" column in the Records Browser.