Getting Started With SuiteScripts
By Shawn Blackburn, NetSuite Administrator, Bandwidth.com
• Get the Latest SuiteScript Developer Guide
• Download an IDE
• Write your code
• Create and Deploy your Script
• Run your Script
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
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
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.
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.