Extending NetSuite with the SuiteScript API
by Thomas Furr
SuiteScript is a JavaScript-based API that gives developers the ability to extend NetSuite beyond the capabilities provided through SuiteBuilder point-and-click customization.
The majority of NetSuite forms, records, customization objects and their event/trigger points are programmatically accessible through SuiteScript. What you decide to do with SuiteScript depends on which part of NetSuite you are trying to extend, search, or process.
What can I do with the SuiteScript API?
When you think about using SuiteScript in NetSuite, you must ask yourself:
1. What do I want to do?
2. Which APIs support what I want to do?
3. How do I run a script in NetSuite?
What do I want to do?
The following are just some of the uses for SuiteScript. Next to each use case is a link to the NetSuite script type you might use to programmatically accomplish the tasks involved. There are hyperlinks in blue in the items below. These link to the NetSuite Help Center so that you can get additional information for specific areas. To access these links you must have a NetSuite Account.
Using SuiteScript you can:
• Perform custom business processing when NetSuite records are updated, created, deleted (using User Event Scripts).
• Perform custom validations and calculations in the browser client (using Client Scripts).
• Create custom user interfaces (using script types such as Suitelets or User Event Scripts and UI Objects).
• Run batch processes (using Scheduled Scripts).
• Execute NetSuite searches (using script types such as User Event Scripts or Scheduled Scripts).
• Perform various utility processing such as sending email and faxes, creating and uploading files, or working with XML documents (using script types such as User Event Scripts or Suitelets).
• Create custom dashboard portlets (using Portlet Scripts).
• Perform processing in target accounts for bundled solutions as part of bundle installation or update (using Bundle Installation Scripts).
In the documentation, the SuiteScript API is organized by the types of tasks most developers want to perform. See SuiteScript API Overview to get started with the SuiteScript API.
See SuiteScript Functions to see how all APIs are organized. The documentation for each API lists whether the API can be used in client, user event, scheduled, Suitelet, or portlets scripts.
How do I run a script in NetSuite?
The overall process for getting a script to run in NetSuite is fairly basic. This process includes:
1. Creating a JavaScript file for your script.
2. Uploading the file into NetSuite.
3. Creating a NetSuite “Script” record for your script.
4. Defining script runtime options on a NetSuite Script Deployment page.
Step 1: Create Your Script
All SuiteScript files must end with a JavaScript (.js) file extension. Although you can use any text editor (including Notepad) to write your SuiteScript .js files, NetSuite recommends you use the SuiteCloud IDE. Using the SuiteCloud IDE will be covered in a future blog.
Depending on what you are trying to do in NetSuite, the code in your .js file can be as simple as a client script that never even touches a NetSuite server. It runs purely client-side in the browser and alerts users after they have loaded a specific NetSuite record, for example:
function pageInitAlertUser()
{
alert (‘You have loaded a record’);
}
Alternatively, your script can be as complex as executing a NetSuite search, getting the results, and then transforming the results into a PDF document.
The APIs you use in your code and the logic you write will depend on what you’re trying to accomplish in NetSuite.
Step 2: Add Script to NetSuite File Cabinet
If you are writing your script files in SuiteCloud IDE, loading a file into the NetSuite file cabinet is as easy as right-clicking on your file in SuiteCloud IDE and selecting NetSuite > Upload Selected File(s).
If you have written your .js files in anything other than SuiteCloud IDE, you will need to manually upload your files into NetSuite. See Uploading SuiteScript into the File Cabinet Without the SuiteCloud IDE for details.
Note:
The SuiteScripts folder in the file cabinet is provided for convenience, however, you can store your script files in any location.
Once your script has been added to the NetSuite file cabinet, see either:
Step 3: Attach Script to Form (if you want to run a form-level client script in NetSuite)
Step 4: Create Script Record (if you want to run any other script type. For example, if you want to run a user event, scheduled, portlet, Suitelet, action, or record-level client script, proceed to Step 4.)
Step 3: Attach Script to Form
Form-level client scripts are simply “attached” to the forms they run against. Be aware that in NetSuite, there are two different types of client SuiteScript. The information in this section pertains ONLY to form-level client scripts.
To attach a form-level client script to a custom form:
1. Ensure your client script has been uploaded into NetSuite.
2. Go to the desired custom form in NetSuite. Form-level client scripts can only be attached to custom entry forms, custom transaction forms, and custom online forms. Click Setup > Customization > [Form].
3. Next, click Customize next to the desired custom form, or click Customize next to an existing standard form in order to create a new custom form based that is based on the standard version.
4. On the Custom Code tab on the form, select your script file and, if necessary, the library file to associate with the current form (see figure below).
5. The library script file should contain any commonly used functions. The SuiteScript file should contain functions specific to the current form.
6. Based on the APIs used in your SuiteScript or library files, define which functions should be called on which client events. If you are unsure of which actions trigger each client event, see Client Event Types.
The following figure shows a custom form called Wolfe Electronics Sales Order. This form is set as the “preferred” form for sales orders. What this means is that all customizations made to this form, and any client script file attached to the form will run whenever a NetSuite user navigates to and loads sales order record. This figure indicates that when a sales order loads, three different functions will execute.
- The savRecUpdatePrice function will execute when the record is saved.
- The valFieldItemPrice function will execute when a particular field on the sales order is changed.
- The recalcTotalAndTax function will execute when a line item has been added to a sublist.
Important:
Be sure to enter function names exactly as they appear in your script. However, do not include parenthesis or parameter values when you enter the function name on the custom form.
After you have attached your form-level client script to a form, your script will execute whenever the triggering action occurs. For a list of possible client event triggers, see Client Event Types.
If you have created a form-level client script, you do not need to go to Step 4: Create Script Record . You are done!!!!!
Step 4: Create Script Record
After writing your SuiteScript .js file and uploading the file to the file cabinet, you must then create a Script record for the file (see figure below).
On the Script record you will:
• Add your SuiteScript .js file.
• Define the script owner.
• If applicable, add one or more library files.
• Define the function(s) from your SuiteScript file you want executed.
• If applicable, create script parameters (custom fields) that are unique to the Script record.
• Specify who should be contacted if an error is thrown in your script.
Although you do not need to set every field on the Script record, at a minimum you must set the following (see figure below):
1. Provide a name for the Script record.
2. Specify the script owner.
3. Load your SuiteScript .js file.
4. Specify the main executing function within the file.
Future Blog
In my next blog, I will take an in depth look at the SuiteCloude IDE!