Getting Started With SuiteScripts

Shawn Blackburn, NetSuite Administrator, Bandwidth.com
363
607
128

Shawn Blackburn, NetSuite Administrator, Bandwidth.com

One of NetSuite’s greatest strengths is its ability to be altered in ways unimaginable to the engineers that build the platform, allowing the ultimate in flexibility. At the heart of NetSuite’s SuiteCloud platform are SuiteScripts – NetSuite talk for JavaScript. NetSuite allows for several different types of scripts. While they are all common in that they share JavaScript as their core language they differ in where and how they can be executed.

"Using JavaScript at the heart of their development platform allows for rapid and cost-effective development when creating or updating system customizations"

Using JavaScript at the heart of their development platform allows for rapid and cost-effective development when creating or updating system customizations. While there are benefits to finding a developer or consultant familiar with NetSuite, any JavaScript developer should be able to develop or modify SuiteScripts. Furthermore, I think a lot of NetSuite Admins have this ability too – Here’s how:

• Take a JavaScript Primer Course
• Get the Latest SuiteScript Developer Guide
• Download an IDE
•    Write your code
• Create and Deploy your Script
• Run your Script

Take a JavaScript Primer Course

JavaScript is a programming language commonly used for client-side web development. A good place to start is the JavaScript tutorial at Code Academy. They offer a free, interactive beginners course that takes around 10 hours to complete.

Get the Latest SuiteScript Developer and Reference Guide (SSDRG)

The SSDRG is NetSuite’s programming Bible. It is chock full instruction, functions, notes, and examples. A new version of the Guide is released coordinating with NetSuite’s twice annual system updates. To get the latest version search 8084 from SuiteAnswers.

Download an IDE

One of the great things about JavaScript is that you need nothing more than a text editor to write JavaScript code. I prefer Visual Studio Code or Sublime Text, although NetSuite has their own more advanced editor called SuiteCloud IDE.

Write Your Code

It seems inevitable that at some time during your stint as a NetSuite Admin you will need to delete several records. Be it a bad CSV import, external integration gone awry, etc., the day will come. For whatever reason, there isn’t a simple efficient way to do this in NetSuite. You can mass delete via Inline Editing, but you're relegated to deleting a few hundred records at a time and repeating. If you need to delete several thousand records this is process could take several hours. Enter the power of SuiteScripts. Over the next few sections, we’ll walk through a simple SuiteScript, deploying, and using it.  

Here’s our script:

Eight lines. Not too bad right? Let me explain what we are doing.

1. We are creating the function. This is what NetSuite calls to execute the script. Our function’s name is “massDelete”. Type and Record are parameters that NetSuite will pass when calling the function. Type will be the record type in NetSuite like Customer, Invoice, Inventory Item, etc. Record will be the Internal ID of the current record.

2. Try lets us issue commands inside the curly brackets and if there is an error keep processing.

3. Let’s delete the record.

4. If something goes wrong we’ll get it with catch. Catch is the counterpart to Try.

5. If we had an error we’ll log it to the NetSuite Script Execution Log. We do this by calling the function nlapiLogExecution.

When you are done typing your code give it an explanative name with an extension of js and save. I’ve named my file massDelete.js.

Create and Deploy your Script Record

For this example, we are going to be using a Mass Update script. If you are not familiar with Mass Updates, they are a powerful tool allowing you to update records with ease. If you can build a saved search, you can build a mass update. Typically though mass updates only allow you to update a predefined set of fields per record and not fields that are disabled via workflows or custom forms. Mass Update scripts get around this limitation.

We need to create a Script Record in NetSuite so it knows where the code is, and how to run it.

1. Navigate to Customization > Scripting > Scripts > New
2. Select Mass Update
3. Fill in the Name field. I set mine to “Mass Delete”
4. It’s a best practice to set your ID. I set mine to “_massdelete”. More can be found in the SSDRG
5. Under the Scripts Tab > Script File, select the + and select your JavaScript file.
6. Under the Scripts Tab > Function, type your function name. For our example it is massDelete
7. Click Save and Deploy

Now We need to Deploy our Script. The Script Deployment tells NetSuite who can run the script.

1.Select the applicable record type from the Applies To tab. This tells NetSuite what kind of records we can delete with our script. You can have multiple deployments per Script Record. For this example let select customer.

2. Select an applicable Role from the Audience tab. For my example, I’m only selecting Administrator.

3. Save

Run your Script

Time to put our new script to use. After navigating to Mass Updates you should have a new option under the Custom Updates Action call Mass Update. This is our script! Clicking into brings up the familiar Saved Search Criteria screen. This is where we tell NetSuite the selection of records we want to delete. Set your criteria and click Preview. The resulting screen shows the list of all the records that are going to be deleted. Once you are happy with the results press the Perform Update button. Please note, that just like everything else in NetSuite there is no Undo button. Anything deleted with this script is gone permanently. Depending on the number of records this could take a few seconds to several minutes to perform.

In eight lines of code, we’ve seen how we can save hours of time harnessing the power of SuiteScripts. Is this a simple example? Sure, but I think that’s the beauty of NetSuite’s Development Architecture. We have the ability to make the scripts as simple or robust as needed to accomplish the task at hand.

Read Also

Now that we can connect everything, should we?

Steven P. Nelson, Executive Director, IOT Freescale Semiconductor

Playing vs. Leading with (Big) Data

Marcus Droege PhD, MBA, Head, Knowledge & Strategic Intelligence, Celgene Corp.

The New Rules of MarTech Integration

Matthew Mobley, EVP & CTO, Merkle