This document will describe the basics of using SuiteScript2 in your extensions for SCA.
- Ensure you select the “SuiteScript2” option when running “gulp extension: create”.
- While you can always add the file later if needed the easiest route is to let the gulp command create it for you.
- An SS2 service is called when the “fetch”, “save”, or “destroy” function is called on a properly formatted Backbone Model.
- “fetch”: This function should be called to initially sync the model with the server.
- It uses an HTTPS “GET” call.
- Data can be passed to the service as part of the request parameters.
- For example:
new ContactManagementMyAccountModel().fetch({ data: { getRole: 'T' } }
- For example:
- “save”: This function should be called to update the model with the server.
- It uses an HTTPS “POST” or “PUT” call.
- Data can be passed to the service as part of the request parameters.
- For example:
this.model.save({ action: 'update', userId: userId });
- For example:
- “destroy”: This function should be called to delete the model on the server.
- It uses an HTTPS “DELETE” call.
- Data can be passed to the service as part of the request parameters.
- For example:
this.model.delete({ action: 'remove', userId: userId });
- For example:
- “fetch”: This function should be called to initially sync the model with the server.
- In order for a Model to call a service the following parameters need to be set on the model:
- 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:
urlRoot: Utils.getAbsoluteUrl( getExtensionAssetsPath("Modules/CustomStoreLocator/SuiteScript2/CustomStoreLocator.Service.ss"), true )
- The path name is the relative path of the service file you want the model to call.
- 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.
- 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:
- In order for the service to work the file needs to be setup in the following ways:
- File name needs to end in “.ss”.
- File must be formatted like other Suitescript 2.x files. For example:
/** * @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 }; });
- The file must return the entry point “service”, which must be mapped onto a function (of any name).
- 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).
- The service file writes data back to the model using
ctx.response.write(JSON.stringify(responseObj)); - This method should only ever be called once per execution of your service.
- A service file will not run until set up correctly in the Netsuite File Cabinet.
- Deploy the extension to get the file into the backend.
- 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.
- Once you have the file record open, enable Permissions on the file. Be sure to “execute as” a role that has the needed permissions.
- Save the file updates. Your service file is now usable (hopefully).
- Any updates to your service file must be made be either deploying your extension again or by manually editing the file record.