This article covers the basics of using SuiteScript2 (SS2) service files in your extensions for SuiteCommerce. A request is made to a service file when certain functions are run on Backbone Models in the front end.
Need a refresher on the basics of SuiteScript 1 vs. SuiteScript 2? Checkout the tutorial article What is SuiteScript?
Ideally, select the “SuiteScript2” option when running “gulp extension:create”. You can, however, always manually add the file(s) later if needed by simply adding .ss files to your “SuiteScript2” folder and Backbone Model files to your “JavaScript” folder as needed.
An SS2 service is called when the “fetch”, “sync”, “save”, or “destroy” function is called on a properly formatted Backbone Model. We will ignore “sync” here, and you likely should ignore it too.
new ContactManagementMyAccountModel().fetch({
data: {
getRole: 'T'
}
}
this.model.save({
action: 'update',
userId: userId
});
this.model.delete({
action: 'remove',
userId: userId
});
Note: 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.
In order for a Model to call a service file, the following properties need to be set on the Model:
urlRoot: Utils.getAbsoluteUrl(
getExtensionAssetsPath("Modules/CustomStoreLocator/SuiteScript2/CustomStoreLocator.Service.ss"),
true
)
For the service to work, the file needs to be set up in the following ways:
NOTE: These requirements only apply to service files, not Suitelets.
/**
* @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
};
});
ctx.response.write(JSON.stringify(responseObj));
This method should only ever be called once per execution of your service.
Like any other SuiteScript, a service file will not run until set up correctly in the NetSuite File Cabinet.
Any updates to your service file must be made be either deploying your extension again or by manually editing the file record (this second option is much easier).
Related Article: Extract Parameters in a Service File | "GET" and "POST" Request
We like to update our blogs and articles to make sure they help resolve any troubleshooting difficulties you are having. Sometimes, there is a related feature to enable or a field to fill out that we miss during the instructions. If this article didn't resolve the issue, please use the chat and let us know so that we can update this article!
If you have questions about how our team can support your business as you implement NetSuite or SuiteCommerce, feel free to contact us anytime. Anchor Group is a certified Oracle NetSuite Alliance Partner and Commerce Partner equipped to handle all kinds of NetSuite and SuiteCommerce projects, large or small!
Tagged with Training