SuiteScript 2.1 Hello World
Become familiar with SuiteScript 2.1 programming basics by creating a simple application that displays a Hello World message.
Step 1: Set Up a NetSuite Account for SuiteScript Development
(2 minutes)
In this step, you will verify that your NetSuite account is set up for SuiteScript development.
If you are not an administrator for the account, contact your NetSuite Administrator and ask them to verify that the account is set up as described in the steps that follow. The administrator must also assign you a Developer user role in NetSuite if you do not already have one.
To set up a NetSuite account for development:
-
Log in to NetSuite as an Administrator user.
-
Select Setup > Company > Setup Tasks > Enable Features.
-
Click the SuiteCloud subtab.
-
Check the boxes for Client SuiteScript and Server SuiteScript.
-
Click I Agree to Terms and Service and then click Save.
-
Optionally, complete the configurations for SuiteCloud Development Framework (SDF). These are not needed for the "Hello World" tutorial. However, the configurations are needed if you plan on using SDF for account customization or SuiteApp projects. For instructions, see Enabling SuiteCloud Development Framework in the Target NetSuite Account (Administrator Only).
Step 2: Create and Edit a SuiteScript File
(5 minutes)
To create and edit the script file:
-
In a text editor, create a new file ( helloWorld.js).
-
Copy the full helloWorld.js script below and paste it into your file. Carefully review the code comments to become familiar with each element in the sample script.
/** * @NApiVersion 2.1 * @NScriptType ClientScript * @NModuleScope SameAccount */ // The above block contains required JSDoc tags. // @NApiVersion is set to SuiteScript 2.1 version, // @NScriptType shows that this is a client script, // and the @NModuleScope value SameAccount indicates that it is for use only in the account where it is created and uploaded. // Use ['N/ui/dialog'] as the define function's first argument; this is an array of string values representing modules the function will load. // The define function's second argument is a callback function; give this function one argument called dialog. define(['N/ui/dialog','N/log'], (dialog, log) => { // Within the callback function, declare a function called pageInit. function pageInit(scriptContext) { // Create the object directly inside the dialog.alert() call. dialog.alert({ title: 'Hello!', message: 'Hello, World' }); log.debug({ title: 'Debug Entry', details: 'Hello, World' }); } return { // Add a return statement after the entry point function; the return statement must include at least one line that has two components: An entry point called pageInit(scriptContext) and a function called pageInit. pageInit: pageInit }; });
For more information about each element covered in this example, see:
-
Save the helloWorld.js file and note its location.
Step 3: Upload the Script File to NetSuite
(2 minutes)
For the Hello World tutorial, you can upload a script to the File Cabinet in NetSuite. But when you advance to creating more complex projects, you will be able to use commands in your integrated development environment (IDE) or the command-line interface (CLI) to upload and deploy projects.
To upload the script file to NetSuite:
-
Log in to NetSuite as a user with a Developer role or developer permissions. If you are not sure if you have this level of access, ask your NetSuite account administrator.
-
Go to Documents > Files > SuiteScripts.
-
Click Add File.
-
In the File Upload dialog, locate and open the helloWorld.js file.
When the upload is completed, you should see the helloWorld.js file listed on the Folder Contents - SuiteScript page.
Tip:You may need to re-upload a script file that you have changed when you are working with your own scripts. If the script file does not show the updates, perform a hard refresh of the page in the web browser
Step 4: Create a Script Record and Script Deployment Record
(3 minutes)
For the Hello World tutorial, you will create a script record and a script deployment record using the NetSuite user interface.
How is a script record different from a script deployment record? A script record identifies the script in NetSuite. A script deployment record determines how and when the script runs.
When you create more complex projects using SuiteCloud Development Framework, you will be able to define the script record and script deployment records using XML files. You will also be able to deploy your projects using SDF commands from your IDE or CLI.
To create a script record:
-
Go to Customization > Scripting > Scripts > New.
-
In the Script File dropdown list, select the helloWorld.js file.
-
Click Create Script Record.
The system displays a new script record, with the helloWorld.js file listed on the Scripts subtab.
-
Enter the script name, for example, helloWorld. However, the name does not need to exactly match the file name of your script file.
-
In the ID field, enter _cs_helloworld.
-
Click Save.
The system creates the script record.
To create a script deployment record:
-
From the script record for your helloWorld script, click Deploy Script. A new Script Deployment page is displayed.
-
From the Applies to list, select Task.
Optionally, you can select a different record type from the list to see the dialog appear when a different type of record loads.
-
In the ID field, enter _cs_helloworld.
The script deployment record can use the same ID suffix as the script record, because the system adds different ID prefixes based on the object type.
-
Leave the other fields set to their default values.
-
Click Save.
The system creates the script deployment record, and the script deployment record shows that the script is deployed. This means that the script can run. Because Status field was left as Testing, the script will only run for the script owner (you) and not for other account users.
Step 5: Run the Script
(2 minutes)
In this step, you will verify that the "Hello!" alert dialog displays the message "Hello, World" when you create a new task record. You will also verify the debug message is logged.
To run the helloWorld script:
-
Open a task record by going to Activities > Scheduling > Tasks > New.
If the script is working properly, the system displays the "Hello!" alert dialog with the message "Hello, World".
-
Confirm and close the dialog by clicking OK, and then click Cancel on the Task page.
-
Verify that the expected log entry has been saved to the script deployment record:
-
Go to Customization > Scripting > Script Deployments.
-
Locate the helloWorld deployment and click View.
-
Click the Execution Log subtab.
If the action was logged correctly, you should see an entry titled Debug Entry, the date and time that the script ran, and the details Hello, World.
If an error occurs, the error will be displayed instead of the debug entry.
-
Leave the deployment record in Testing status.
If this were a customization that you wanted to make available to all users, you would edit the deployment record and set the status to Released, and then click Save.
-
Next Steps
You have deployed your first script. Consider browsing other topics within the SuiteScript 2.x API Introduction to learn more.
To experiment with other script samples, see:
-
A complex user event script, SuiteScript 2.x User Event Script Sample.
-
A user event script for server scripts that executes when users take actions with records, SuiteScript 2.x User Event Script Tutorial.
-
A custom module script and a user event script that calls a custom module function, SuiteScript 2.x Custom Module Tutorial.