SuiteScript2 Services for SCA

This document will describe the basics of using SuiteScript2 in your extensions for SCA.

  1. Ensure you select the “SuiteScript2” option when running “gulp extension: create”.
    1. While you can always add the file later if needed the easiest route is to let the gulp command create it for you.
  2. An SS2 service is called when the “fetch”, “save”, or “destroy” function is called on a properly formatted Backbone Model.
    1. “fetch”: This function should be called to initially sync the model with the server.
      1. It uses an HTTPS “GET” call.
      2. Data can be passed to the service as part of the request parameters.
        1. For example:
          1. new ContactManagementMyAccountModel().fetch({ data: { getRole: 'T' } } 
    2. “save”: This function should be called to update the model with the server.
      1. It uses an HTTPS “POST” or “PUT” call.
      2. Data can be passed to the service as part of the request parameters.
        1. For example:
          1. this.model.save({    action: 'update',    userId: userId }); 
    3. “destroy”: This function should be called to delete the model on the server.
      1. It uses an HTTPS “DELETE” call.
      2. Data can be passed to the service as part of the request parameters.
        1. For example:
          1. this.model.delete({    action: 'remove',    userId: userId }); 
  3. In order for a Model to call a service the following parameters need to be set on the model:
    1. The “urlRoot” must be properly set. This parameter points to the service file using a relative path. The “urlRoot” is set by default when the Model file is created by the gulp command. It should look something like:
      1. urlRoot: Utils.getAbsoluteUrl(             getExtensionAssetsPath("Modules/CustomStoreLocator/SuiteScript2/CustomStoreLocator.Service.ss"),             true         ) 
    2. The path name is the relative path of the service file you want the model to call.
    3. Lastly, while you could technically overwrite the “fetch”, “save”, and “destroy” functions for your model this will likely interfere with calling the service, so be careful in doing so.
  4. In order for the service to work the file needs to be setup in the following ways:
    1. File name needs to end in “.ss”.
    2. File must be formatted like other Suitescript 2.x files. For example:
      1. /** * @NApiVersion 2.x * @NModuleScope Public */ define(['N/record','N/runtime'],function (record,runtime) { "use strict"; function service(ctx) { var responseObj = {}; var params = ctx.request.body ? JSON.parse(ctx.request.body): {}; var getParams = ctx.request.parameters; try{ var userObj = runtime.getCurrentUser(); if(getParams.getRole == 'T'){ responseObj.role = userObj.role; } } catch(e){ responseObj.error = e; } ctx.response.write(JSON.stringify(responseObj)); } return { service: service }; }); 
    3. The file must return the entry point “service”, which must be mapped onto a function (of any name).
    4. The sole argument of the service function, often written as “context”, includes important info such as the incoming data and type of call (GET, POST, etc).
    5. The service file writes data back to the model using ctx.response.write(JSON.stringify(responseObj));
    6. This method should only ever be called once per execution of your service.
  5. A service file will not run until set up correctly in the Netsuite File Cabinet.
    1. Deploy the extension to get the file into the backend.
    2. Navigate to the file located here: Commerce > Hosting > Website Hosting Files > Live Hosting Files > SSP Applications > NetSuite Inc. – SCA 2021.2.0 (or whatever version you are using) > Development2 > extensions – then dig down to find your extension’s service file.
    3. Once you have the file record open, enable Permissions on the file. Be sure to “execute as” a role that has the needed permissions.
    4. Save the file updates. Your service file is now usable (hopefully).
  6. Any updates to your service file must be made be either deploying your extension again or by manually editing the file record.

Leave a comment

Your email address will not be published. Required fields are marked *