In the initial post for this scripting tutorial, we took a look at the basics of SuiteScript 2.0. Now it’s time to create your very first working NetSuite SuiteScript 2.0 script and see it in action in NetSuite. The main purpose of this tuturial blog is to help you understand the steps necessary to upload a script into NetSuite and deploy it. I’ll keep the code pretty simple so that it’s easy to follow along, and we will build on it in future tutorials.

define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function fieldChanged(context) {
        // do things
    }
    
    function saveRecord(context) {
        // do things
    }
    
    return {
        fieldChanged: fieldChanged,
        saveRecord: saveRecord
    }
    
});

Create A Hello Word Alert Box

Create A JavaScript File

First of all, you need to create a JavaScript file and give it a name. It’s always good to figure out a naming convention that helps you stay organized. Personally, I like to include “sr” for SuiteRep, the type of script being created which in this case is “cli” for client script, and a description of what the script does, “helloworld.” So putting that together, I would name the JavaScript file “sr_cli_helloworld.js.”

Once you have created the file, you can open it in any text editor. My favorite is Visual Studio Code which can be downloaded and installed for free.

At this point you can copy and paste the code included above as your starting point.

Set up a Trigger function

For this hello world script, we want an alert box to appear when you start to edit a customer record. So we would need to use the pageInit trigger. Rename one of the functions to “pageInit” and make sure to associate the “pageInit” function with the pageInit trigger in the return section. Then get rid of the other function. This is how your code should look after making those changes:

define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        // do things
    }
    
    return {
        pageInit: pageInit
    }
    
});

Add the Alert Box

The JavaScript function for showing an alert box is just alert(). And the message on the alert will go inside the parentheses like this: alert(“My first SuiteScript!!”). You will need to add that inside your “pageInit,” and your code should now look like this:

define(['N/record','N/search'], function (record, search) {
    /**
    *@NApiVersion 2.0
    *@NScriptType ClientScript
    */
    
    function pageInit(context) {
        alert("My first SuiteScript!!");
    }
    
    return {
        pageInit: pageInit
    }
    
});

Add your Script to NetSuite

That is all the coding that will be needed for this script. Now you just need to add it to NetSuite and deploy it.

  1. Make sure to save the JavaScript file somewhere on your computer where you can easily navigate to it.
  2. Log into NetSuite and Navigate to Customization > Scripting > Scripts > New.
  3. On the “Upload Script File” screen, click on the plus sign to the right of the Script File field to add your JavaScript file.
  4. On the “File” page, click on the “New File” button and navigate to your JavaScript file to upload it. Then Save that page.
  5. Now back on the “Upload File” screen, click on the “Create Script Record” button.
  6. Now you’re on the “Script” page. Give your script a name (e.g. Hello World) and an ID. I usually use the same naming convention for the ids. So mine would look like _sr_cli_helloworld. You always want to lead with an underscore because “customscript” will be added to the beginning of whatever you put as the id.
  7. Click on the “Deployments” tab.
  8. Select “Customer” in the “Applies To” column. This will allow the script to run on the customer record.
  9. Make sure the “Deployed” column is “Yes.” If you set it to “No,” the script will not run.
  10. Make sure the “Status” column is set to “Testing.” That will cause the script to run only for you and no one else. If you set it to “Release,” it will run for everyone.
  11. Save the script.

Conclusion

Now you’re ready to test. Navigate to a customer record and click edit. As soon as the customer record loads, you should see the alert with the message you gave it. And you’ve just created your first working SuiteScript 2.0 NetSuite script!

If you keep following along with these tutorials you will learn how to create some pretty exciting things. You can access the next blog in this series here. And don’t forget to subscribe to our mailing list so you can receive our latest developing blogs directly in your inbox each week!