What are Modules?

Modules are at the core of what allows developers to work with NetSuite. Without modules, there would be no such thing as SuiteScript 2.0. Modules contain the API information needed for scripting. We go into greater detail about the benefits of modularity in our post, 3 Reasons You Should Switch from SuiteScript 1.0 to 2.0.

In this SuiteScript tutorial, we will turn our attention to learning about the different modules in NetSuite. In the beginning, we will explore some of the most important modules that are used regularly in the SuiteScript world. At a later time, we may explore some of the interesting modules that often are neglected.

Did somebody mention modules?

  • N/action Module
  • N/auth Module
  • N/cache Module
  • N/certificateControl Module
  • N/commerce Modules
  • N/compress Module
  • N/config Module
  • N/crypto Module
  • N/crypto/certificate Module
  • N/currency Module
  • N/currentRecord Module
  • N/dataset Module
  • N/email Module
  • N/encode Module
  • N/error Module
  • N/file Module
  • N/format Module
  • N/format/i18n Module
  • N/http Module
  • N/https Module
  • N/https/clientCertificate Module
  • N/keyControl Module
  • N/log Module
  • N/piremoval Module
  • N/plugin Module
  • N/portlet Module
  • N/query Module
  • N/record Module
  • N/recordContext Module
  • N/redirect Module
  • N/render Module
  • N/runtime Module
  • N/search Module
  • N/sftp Module
  • N/sso Module
  • N/task Module
  • N/task/accounting/recognition Module
  • N/transaction Module
  • N/translation Module
  • N/ui Modules
  • N/url Module
  • N/util Module
  • N/workbook Module
  • N/workflow Module
  • N/xml Module

Are you overwhelmed yet? Thankfully only a handful of these are crucial to know. Think of modules like you might think of a language. In English, there are certain words that are used incessantly—interjections like and, but, and the, along with some of our most common verbs, nouns, and adjectives. It is not crucial that a child learn such complex words as juxtaposition, onomatopoeia, or pneumonoultramicroscopicsilicovolcanoconiosis. It is true that some people in the world, such as doctors, writers, or bloggers, need to know such words, but fewer words are needed to begin relating with the real world. And as the SuiteScript developer advances and spreads his wings, he will soon begin to explore the possibilities that are possible with NetSuite scripting.

Tip in how to use modules effectively:

The help documentation is extremely helpful. Use the leftmost list there to navigate around and see how things relate. Becoming fluent in using the help documentation is crucial to effective Netsuite development. Such fluency can be achieved only through consistent practice.

 

The N/record Module

The Record module enables use to manipulate native Netsuite records (unfortunately, this will not work with custom records). “Manipulate” means that one can create, delete, copy, load, or modify a record.

Here are some important things to note:

  • The Record module can be used in both client and server side scripts. The only exception is for function calls that use Promises. (As of Suitescript 2.0, these are limited to Client side only, although this will likely change in the future.)
  • Typically, the primary use of the record module is for server-side scripts and some client scripts which must manipulate a record other than the one it is currently deployed/active on.

SuiteScript Example:

The following example shows how to retrieve information from a Sales Order, and then create a new Sales Order using the Record Module from that retrieved information.

define(['N/record'], function (record) {
    /**
     * Example script to test the Record module. Copies essential information from an SO and creates a new SO from that info.
     * 
     * Deploy To: Sales Orders
     * @NApiVersion 2.x
     * @NScriptType UserEventScript
     * @author Ben Rogol (SuiteRep)
     */
    var exports = {};

    /**
     * @function afterSubmit Function to be executed after record is submitted to server.
     * @governance XXX
     * 
     * @param {Object} context 
     * @param {Record} context.newRecord  Record as submitted to server
     * @param {Record} context.oldRecord  Record as loaded from server
     * @param {String} context.type  The type of UE with which the record is being accessed (create, copy, edit)
     * 
     * @return {void}
     * @since 2015.2
     */
    function afterSubmit(context) {
        log.debug("context.type", context.type); // If SO is being created, returns "create"; if being edited, returns "edit"
        if (context.type != 'create') { // The script will only run if a SO is being created
            log.debug("Record not being created. Script will not copy record.");
            return; 
        }

        // Retrieving info from original SO
        var date = context.newRecord.getValue({ fieldId: 'trandate' }); // Getting important field values...
        var entity = context.newRecord.getValue({ fieldId: 'entity' });
        var lineCount = context.newRecord.getLineCount({ sublistId: 'item' })

        var items = [];
        var qtys = [];
        var amounts = [];

        for (var i = 0; i < lineCount; i++) { // Getting all the sublist values
            var item = context.newRecord.getSublistValue({
                sublistId: 'item',
                fieldId: 'item',
                line: i
            });
            var qty = context.newRecord.getSublistValue({
                sublistId: 'item',
                fieldId: 'quantity',
                line: i
            });
            var amount = context.newRecord.getSublistValue({
                sublistId: 'item',
                fieldId: 'amount',
                line: i
            });

            items.push(item);
            qtys.push(qty);
            amounts.push(amount);
        }

        // *** USING THE RECORD MODULE ***
        var newSalesOrder = record.create({ // Creating a new SO
            type: record.Type.SALES_ORDER,
            isDynamic: true // Important: Dynamic Mode
        })
        newSalesOrder.setValue({ // Setting the date
            fieldId: 'trandate',
            value: date
        })
        newSalesOrder.setValue({ // Setting the entity (company)
            fieldId: 'entity',
            value: entity
        })

        for (var i = 0; i < items.length; i++) {
            newSalesOrder.selectNewLine({ // Select line to input info to
                sublistId: 'item'
            });
            newSalesOrder.setCurrentSublistValue({ // Set item for line
                sublistId: 'item',
                fieldId: 'item',
                value: items[i]
            });
            newSalesOrder.setCurrentSublistValue({ // Set qty for line
                sublistId: 'item',
                fieldId: 'quantity',
                value: qtys[i]
            });
            newSalesOrder.setCurrentSublistValue({ // Set amount for line
                sublistId: 'item',
                fieldId: 'amount',
                value: amounts[i]
            });
            newSalesOrder.commitLine({ // Commit the line
                sublistId: 'item'
            });
        }

        try {
            var newSalesOrderID = newSalesOrder.save(); // Attempting to save the record
            log.debug("New Sales Order ID", newSalesOrderID)
        } catch (e) {
            log.error("Not able to save new Sales Order - " + e.name, e.message);
        }
    }

    exports.afterSubmit = afterSubmit;
    return exports;
});

Conclusion

There are lots of other things the Record Module can do. We recommend checking out the NetSuite help documentation to see all the possibilies this module provides.

We hope this helped you get a taste of what can be done with the “N/record” module in SuiteScript. Keep up to date with our latest development blog posts by signing up to our email list!