SuiteScript Code Structure

NetSuite for Developers

Having a standard method for your SuiteScript code structure can be extremely helpful. Having a set structure provides consistency, preemptively removes difficult roadblocks, and reduces the number and severity of bugs.

A Structural Overview

This is a cutting-edge thought-process for us at SuiteRep. In the past, we have generally worked by the principle of using the least number of JavaScript files as possible, only creating new ones when absolutely necessary. But as time went on, we realized the need for something more coherent and intentional. Unlike previous posts about SuiteScript fundamentals, the current domain is a bit more subjective. Different developers will have different techniques. Here, we try to provide a potential solution that we are finding effective.

Here’s an overview. As the saying goes, a picture says a thousand words (or in this case, maybe ten-thousand)!

One Script to Rule Them All

Highlighted in yellow are the foundations for a User Event or Client script coding project (“sr_recautomations_so_ue.js” and “sr_recautomations_so_cli.js”). However, notice the name “_recautomations_” (in either the UE or CLI scripts). This is a very generic description, separate from the specific project being coded (the ficticious “_autoemail_” project). Here’s what we’re trying to do: we are trying to limit all User Event or Client script automations to one script type per record (in this case a Sales Order record, “_so_”). This one script will handle all automations related to that record by simply calling functions from other libraries (thus the “Other Libraries” blocks in the diagram).

Organizing your SuiteScript code structure in this way has the following notable benefits:

  1. Resolves situations where multiple User Event or Client Scripts overlap and cause strange bugs in the system.
  2. Allows us to put a try/catch safeguard around entire projects so nothing catastrophic ever happens with our scripts.
  3. Allows us to use libraries (the heart of the automations) with greater flexibility across a variety of scripts.
  4. Reduces clutter and simplifies NetSuite automations.

Libraries

Libraries (or “custom modules”) thus become the heart of all NetSuite automations. In the diagram above, we have 3 types of custom libraries—(1) Server Libraries, (2) Client Libraries, and (3) Main Libraries.

When a library is loaded into another script, it essentially becomes part of that script. Because of this, we cannot call a Client library into a User Event script if that library uses modules and methods exclusive to Client scripts. We may get an error if we attempt to do so. But often there are things in common between the two libraries (like the search module for instance). These blocks of code should go in the “Main Library.”

Enums

Enums provide all the nitty-gritty details for the script libraries. They detail things like hard-coded values, field IDs, and much more. Using enums effectively can actually dramatically reduce the size and increase the readability of your scripts. Stay tuned for future blog posts about using enums efficiently.

Suitelets

And lastly, there are two primary things to note about Suitelets. First, they can reference both a Server Library (directly) and a Client Library (through the “ClientScriptModulePath” function). Being able to reference libraries in the Suitelet and the Client script handler will greatly simplify your code. Few things are more difficult than looking at a colossal and messy Suitelet script that has everything piled in together! Codes like this can be very challenging to debug or adjust. But having separate libraries and enums can make debugging or adjusting projects a 20-second matter in some cases. The second thing to note about Suitelets is the possibility to connect with https.get(). If we need to call a Suitelet from a User Event or Client automation, we can call the Suitelet using this method. Our 3 libraries can also be shared between the two sides if desired.

Conclusion

Improving SuiteScript code structure is an area where all SuiteScript developers need to grow continually. We hope this article helps provide some ideas. What method have you found helpful for you or your SuiteScripting team? Comment down below to let us know. And as always, don’t forget to subscribe to our email list to stay up to date with our latest posts!