2. Agenda
NetSuite at a glance
What is SuiteScript API?
Key concepts of SuiteScript 2.0 API
SuiteScript 2.0 Syntax
HelloWorld Script
Advantages of SuiteScript 2.0
Drawback
Coexistence rules
1
3. Objectives of this presentation
Understanding the basics of SuiteScript 2.0 API
Explore the key differences between SuiteScript 1.0 & SuiteScript 2.0
2
4. NetSuite at a glance
Cloud-based business management software
Software as a Service (SaaS)
3
5. Is a JavaScript API that offers a broad range of options
for enhancing and extending NetSuite
SuiteScript API
What is SuiteScript API?
😟 😎 4
7. SuiteScript 2.0 is modular
All SuiteScript 2.0 APIs are organized into modules
Each module reflects the functionality
Concept #1:
6
8. NetSuite modules & objects
record searchlog file
NS
N
create({}) save({})load({})
∼32 modules
e.g: N/file
∼… APIs
e.g: record.create({});
setValue({}) …({})
Concept #1 (cont’d)
7
Module must be explicitly loaded by a script before using that module’s API
9. Suitescript 1.0 API
Organized in a Single global library JavaScript file.
Each file gets loaded to every single script regardless of how much API the script use.
Suitescript 2.0 API
Organized and grouped into the modules
Modules are loaded only when they are needed. — based on Asynchronous
Module Definition(AMD)
Now
Before
Modular
Concept #1 (cont’d)
8
10. Object as Arguments
The arguments passed to methods are typically {key:value} objects
var myObject = {
fieldId: 'greetingMsg',
value: 'Hello, World!'
};
myRecord.setValue(myObject);
myRecord.setValue({
fieldId: 'greetingMsg',
value: ‘Hello, World!'
});
Concept #2:
9
11. Suitescript 1.0 API
Argument list
Suitescript 2.0 API
Object as argument
Now
Before
‣ Objects
var myObject = {
fieldId: 'greetingMsg',
value: 'Hello, World!'
};
myRecord.setValue(myObject);
nlapiSetFieldValue('greetingMsg', 'Hello, World!');
Concept #2 (cont’d)
10
12. Script types and their Entry pointsConcept #3:
Script type:
SuiteScript 2.0 scripts consist of several script types
Each script type is designed for a specific type of situation and specific
types of triggering events
Entry point:
Represents the juncture at which the system grants control of the NetSuite application to
the script.
Each script type includes one or more entry points that are exclusive to that type
11
14. Suitescript 1.0 API
Scripts are not Expressive: - hard to recognize the purpose of a script
Scripts are Dependent: - Actions depend on settings done on NetSuite side
Suitescript 2.0 API
Expressive scripts - automatically detected by NetSuite
Independent scripts - No other enhancements needed.
Now
Before
‣ JSDocs tags
‣ Script types & Entry points
function myFunction(){
// logic here
}
sample_script.js
/**
* @NScriptType <script_type_name>
*/
define([], function() {
function myFunction(){
// logic here
}
return {
<entry_point_name> : <entry_point_function>
};
});
sample_script.js
Concept #3 (cont’d)
13
15. Entry point scripts & Custom module scriptsKey concept #4:
Entry point script:
The primary script attached on the script record
It identifies the script type, entry points, and entry point functions
Each entry point script must include at least one entry point and entry point function
Custom module script:
Is a user-defined script that holds the logic that can be used by other scripts
It’s loaded by an Entry point script as a dependency
Script type(s) declaration is not required
14
16. Suitescript 1.0 API
Suitescript 2.0 APINow
Before
NS
Script record A Script record B
script_A.js script_B.jsscript_A.js script_B.js
NS
Script record A Script record B
script_A.js script_B.js
‣ Custom module scripts
Concept #4 (cont’d)
15
Same modules used by-
many script records
17. Anatomy of an Entry Point Script
/**
* @NApiVersion 2.0
* @NScriptType UserEventScript
*/
define(['N/record', 'N/ui/dialog'],
function(record, dialog) {
function doBeforeLoad() {
/* logic of function*/
}
function doBeforeSubmit() {
/* logic of function*/
}
function doAfterSubmit() {
/* logic of function*/
}
return {
beforeLoad : doBeforeLoad,
beforeSubmit: doBeforeSubmit,
afterSubmit : doAfterSubmit,
};
}
);
JSDoc tags:
Required for an entry point script
Entry points:
At least one STANDARD entry point is required for
a script type
Define’s first argument:
List of Dependencies and/or Modules
3
2
1
4
Define’s second argument:
Callback function
1.
2.
3.
4.
Concept #4 (cont’d)
16
18. Anatomy of a Custom Module Script
JSDoc tags:
NOT Required for a custom module script, but USEFUL.
Entry point:
At least one USER-DEFINED entry point is required for a
custom module
Define’s first argument:
List of Dependencies and/or Modules required by the script
/**
* fileName.js
* @NApiVersion 2.x
* @ModuleScope Public
*/
define(['N/record', ’N/search'],
function(record, search) {
function dummySearch() {
/* logic of function*/
}
return {
myDummySearch : dummySearch,
};
}
);
3
2
1
4
Define’s second argument:
Callback function
1.
2.
3.
4.
Concept #4 (cont’d)
17
20. SuiteScript 2.0 Syntax
SuiteScript 2.0 syntax == JavaScript Syntax
๏ SuiteScript 2.0 methods takes plain JavaScript key/value object as an input.
๏ All boolean values take true or false (not T or F respectively)
๏ Parameter types in SuiteScript 2.0 must match as those listed in the Docs/NetSuite help
๏ Enumeration encapsulated common constants (e.g; standard record types)
๏ Sublists and column indexing begins at 0 (not 1)
… except some rules:
19
21. Let’s create a simple entry point script:
✓ Shows a message on page load
/**
* @NApiVersion 2.0
* @NScriptType ClientScript
*
*/
define([ 'N/ui/message' ], function(message) {
function showMessage() {
var msgObj = message.create({
title : 'Greeting',
message : 'Hello, welcome!',
type : message.Type.INFORMATION,
});
msgObj.show();
}
return {
pageInit : showMessage,
};
});
• Q: Why a Script Type is ClientScript ?
• Q: What is the name of an entry point
in this script ?
A: “shows a message on page”
(does something on a page)
A: “pageInit”
• Q: What is the name of an entry point
function in this script ?
A: “showMessage”
20
23. Some Advantages of SuiteScript 2.0
Enables us to create our own custom modules
✓ Keeps code DRY (Don’t Repeat Yourself): - One module used by many scripts!!
✓ Abstraction : - Later changes don’t affect the deployed script(s)
✓ Possible to add those modules to SuiteApps and expose them to third parties.
Modern programming syntax and behavior
✓ Modeled to behave and look like modern JavaScript — e.g: No nlapi/nlobj prefix
✓ Third party JavaScript API support
✓ Updated sublist and column indexing — (begins at 0 instead of 1)
Functionality enhancements
✓ MapReduce script type : designed to handle large amounts of data.
‣ automatic yielding without disruption to the script
Automatic Dependency management: No need to remember to order of library files
22
24. Other Advantages of SuiteScript 2.0
Easier to catch-up for non-NetSuite programmers
Easier to upgrade for future versions: easy as changing the
version number
Good and solid documentation with adequate examples
SuiteScript 2.0 the way forward
23
25. Optional parameters
[e.g; setAge(age = 18)]
Drawback
JavaScript SuiteScript 2.0
Arrow functions
[e.g; (a, b) => a + b;]
Rest parameters
[e.g; avgAge(…age)]
Block-scoped variables
[ e.g; let age = 18; ]
Mismatching of Scripting-language specification:
- JavaScript’s new version is ECMAScript 8 (partially supported), but SuiteScript 2.0 currently
supports ECMAScript 5
There’s a workaround here!, but haven’t tried it yet!! 24
Ex.:
26. Coexistence rules
1. SuiteScript 1.0 and 2.0 cannot intermix within the same script
‣ You cannot use 2.0 modules as 1.0 Library scripts
‣ You cannot include 1.0 files as 2.0 dependencies
2. The two versions can intermix in the same account, in the same
application, and can even be deployed on the same record.
SuiteScript 1.0 is still supported. however…
25
…however, SuiteScript 1.0 can be deprecated anytime!!!.