The currentRecord module is one of the most vital in SuiteScripting. In this tutorial, we will show some of the most important things to know as you begin using this module.

Similar to the record module, the currentRecord module is one of the most commonly used modules in SuiteScript 2.0. Unlike the record module, however, the currentRecord module can be a bit confusing to understand at times. Let’s get a few things settled here first:

1. The currentRecord module can only be used in Client-side scripts. No User Event scripts or Suitelets allowed here.

2. This module is best used when manipulating a record on which a client script is currently deployed and active. For manipulating a different file in the system, use the ‘N/record’ module.

3. The currentRecord module is always in dynamic mode. This is very important remember because all the script executions must be in dynamic mode.

4. In an entry-point client script, the module does not need to be defined in the script, since it is already assumed. You will still need to include the context as a parameter of the entry-point function, however (see example below).

5. In a custom module/library client script, the ‘N/currentRecord’ module must be defined.

What is Dynamic Mode?

Here’s my quick definition of dynamic mode:

Dynamic mode, as opposed to Standard Mode, is a mode of SuiteScripting where every script execution mirrors the user interface.

Dynamic mode is especially important to consider when manipulating sublists. For example, if you wanted to change a sublist field value using currentRecord in the user interface (like quantity, for instance), you would first select the line, then change the value, and finally save/commit the line. Dynamic mode scripting works the same way. Here’s a code example:

Coding in Dynamic Mode

function pageInit(context) {
    var currentRecord = context.currentRecord;

    currentRecord.selectNewLine({
        sublistId: 'item'
    });

    currentRecord.setCurrentSublistValue({
        sublistId: 'item',
        fieldId: 'quantity',
        value: '5',
        ignoreFieldChange: true
    });

    currentRecord.commitLine({
        sublistId: 'item'
    });
}

As you can see, we must first select the specific sublist line before editing the value. After that, we must also commit the line for the value to “stick.” We also defined currentRecord based on the context. It is possible to always use “context.currentRecord” whenever you need to use it.

You may be more comfortable or familiar with the mode called “Standard Mode.” What does Standard Mode look like? I’m glad you asked.

Coding in Standard Mode

context.newRecord.setSublistValue({ 
    sublistId: 'item', 
    line: 1, 
    fieldId: 'quantity', 
    value: '5' 
})

Standard mode may be what you are more familiar with, but dynamic mode is also quite intuitive. The help documentation will be very helpful as you begin coding in dynamic mode. Be sure to check whether the API call you are making supports dyanamic mode, standard mode, or both.

SuiteScript Example Using CurrentRecord

The following example demonstrates (1) how to import currentRecord and use it in a custom library and (2) how to use it in an entry-point function natively.

define(['N/currentRecord'], function (currentRecord) { // <-- HERE
    /**
     * Test script showing how to import and use "N/currentRecord"
     * 
     * @NScriptType ClientScript
     * @NApiVersion 2.x
     * @author Ben Rogol (SuiteRep)
     * 
     */
    var exports = {};

    // Using a non-entry-point function (import for custom library)
    function testCurrentRecord() {
        var cr = currentRecord.get(); // <-- HERE

        for (var i = 0; i < cr.getLineCount('item'); i++) {
            cr.selectLine({ 
                sublistId: 'item', 
                line: i 
            });
            cr.setCurrentSublistValue({ 
                sublistId: 'item', 
                fieldId: 'quantity', value: '5' 
            })
            cr.commitLine({ 
                sublistId: 'item' 
            })
        }
    }
    
    // Using an entry-point function (has currentRecord natively)
    function pageInit(context) {
        var cr = context.currentRecord; // <-- HERE
        for (var i = 0; i < cr.getLineCount('item'); i++) {
            cr.selectLine({ 
                sublistId: 'item', 
                line: i 
            });
            cr.setCurrentSublistValue({ 
                sublistId: 'item', 
                fieldId: 'quantity', value: '5' 
            })
            cr.commitLine({ 
                sublistId: 'item' 
            })
        }
    }

    exports.testCurrentRecord = testCurrentRecord;
    exports.pageInit = pageInit;
    return exports;
});

Conclusion

We hope you found this SuiteScript tutorial helpful! Next week, we will continue our series on SuiteScript 2.0 modules. Be sure to subscribe to our email list to stay up to date on our latest blogs!