Step 1: Create a Custom [Module].ServiceController

Documentation article about Step 1: Create a Custom [Module].ServiceController

·3 min read·View Oracle Docs

Step 1: Create a Custom [Module].ServiceController

Services handle HTTP requests sent by the frontend application to access backend models, which connect to NetSuite. Different releases of SuiteCommerce Advanced (SCA) handle services with significant differences. This section explains how to create a service when implementing the Vinson release of SCA or later.

  • 2019.2 implementations and later provide two options: SuiteScript 2.0 or SuiteScript 1.0. How you go about this depends on the services you are creating. These implementations require Service Controllers.

    Note:

    The following code samples use SuiteScript 1.0 functions and objects only. These examples do not include SuiteScript 2.0 capabilities.

  • SCA implementations of Vinson through 2019.1 require only SuiteScript 1.0. These implementations require Service Controllers.

Note:

If you are using custom services that pre-date the Vinson release of SCA, you can still use them without refactoring your code. SCA offers backward compatibility with any services of implementations prior to Vinson. See Using Pre-Vinson Services with Vinson Release or Later for more information.

Much of the logic required to operate a service is contained in a centralized location within the source files provided. However, you must complete the following steps to create a custom service:

Step 1: Create a Custom [Module].ServiceController

Each service requires a unique [Module].ServiceController. You create this file, where [Module] is the module using the service. To create this custom ServiceController, complete the following tasks:

  1. Create a new [Module].ServiceController file

  2. Define the custom [Module].ServiceController and dependencies

  3. Extend ServiceController

  4. Add custom Validation Options (Optional)

  5. Add HTTP methods (as required for the service)

Create a new [Module].ServiceController file

  1. Create a new file titled [Module].ServiceController.js, where [Module] is the module associated with the service.

  2. Place this service controller file in the module's SuiteScript directory.

Define the custom [Module].ServiceController and dependencies

  1. In your new [Module].ServiceController.js file, define the specific ServiceController and any dependencies.

  2. Include ServiceController as a dependency. How you do this depends on your implementation.

The following code defines the Account Login ServiceController and enables the service to access methods defined in ServiceController and Account.Model.

javascript
1// Account.Login.ServiceController.js - Service to handle the login of a user into the system
2"text-purple-400">define(
3 'Account.Login.ServiceController'
4, [
5 'ServiceController'
6 , 'Account.Model'
7 ]
8, "text-purple-400">function(
9 ServiceController
10 , AccountModel
11 )

Extend ServiceController

  1. [Module].ServiceController must extend ServiceController and include the name property. This property is used when listening to an event. The value of the name property should match the name of [Module].ServiceController.

  2. Use the following example as a guide. The example shows this extension with the name property.

    javascript
    {
    'use strict';
    "text-purple-400">return ServiceController.extend({
    name:'Account.Login.ServiceController'

Add custom Validation Options (Optional)

  1. If you need to define any validation options for the service, include these within return ServiceController.extend.

  2. Use the following example as a guide. This example defines a requirement for the HTTP protocol to be secure before operating this service.

    javascript
    , options: {
    common: {
    requireSecure: true
    }
    }

Add HTTP methods

  1. Within the object returned, define the functions required to handle the HTTP methods for the service. This code reads the HTTP method sent from the frontend (POST, DELETE, GET or PUT).

    For your [Module].ServiceController to function, you must declare each method in lowercase and it must match the name of the HTTP method being used.

  2. Use the following example as a guide. This example defines what the service does on a POST request.

    javascript
    , post: "text-purple-400">function()
    {
    "text-purple-400">return AccountModel.login("text-purple-400">this.data.email, "text-purple-400">this.data.password, "text-purple-400">this.data.redirect)
    }

Using these examples, your custom [Module].ServiceController might look something like

Frequently Asked Questions (4)

Does creating a custom ServiceController apply to both SuiteScript 1.0 and 2.0?
In SuiteCommerce Advanced (SCA) implementations from 2019.2 onwards, you have the option to use either SuiteScript 1.0 or 2.0 for creating custom ServiceControllers. However, the given examples only use SuiteScript 1.0.
What file naming convention should be used for a custom ServiceController?
The custom ServiceController file should be named [Module].ServiceController.js, where [Module] is the name of the module associated with the service.
How do I define dependencies in a custom ServiceController file?
Dependencies in a custom ServiceController file should be defined using the define function, listing each dependency in an array. For instance, the ServiceController and Account.Model can be included as dependencies.
What happens if I need to handle secure HTTP protocols in my custom ServiceController?
You can include validation options in the ServiceController to handle secure HTTP protocols by defining the 'requireSecure' option within 'options' in return ServiceController.extend.
Source: Step 1: Create a Custom [Module].ServiceController Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in General

View all General articles →