It is often required to add/update the backend server code in order for an extension to function properly. Unfortunately, backend code must be written using SuiteScript 1.0. This complicates things. Here's how you can add listeners to the backend “Application” object in order to effectively extend the native SuiteCommerce code.
Updating the backend code can have many benefits:
Listeners are most often added in an extension’s “SuiteScript” entry point. In your extension manifest this would be the file located here:
"ssp-libraries": {
"entry_point": "Modules/MyExtension/SuiteScript/MyExtension.Entry.js",
"files": [...]
},
This file runs on Application load and allows you to add listeners before most other code runs. Below, you will see examples of adding listeners. Read the code carefully to understand how it works.
define('MyExtension.Entry'
, [
'Application',
'Account.Model',
'underscore'
]
, function (
Application,
AccountModel,
_
)
{
'use strict';
//! Use "Application.on" to add listeners. You will see multiple examples below
//Add a function that runs "after" the "ProfileModel" "get" function is run.
Application.on('after:Profile.get', function (Model, profile) {
//Add custom logic here to change/add data that is returned to the front end Profile Model
//the "Model" arg is the backend Profile Model
//the "profile" arg is the object returned from the native "get" function
return profile;
});
//! NOTE: Wrapping can still work in some cases, but don't assume it will.
AccountModel.login = _.wrap(AccountModel.login, function (fn) {
var params = _.toArray(arguments).splice(1);
var returnObj = fn.apply(this, params);
return returnObj;
});
//Add a function that runs "after" an "InvoiceModel" "setExtraListColumns" function is run.
//this example function adds a search column
Application.on("after:Invoice.setExtraListColumns", function(Model) {
//the "Model" arg is the backend Invoice Model
Model.columns.new_column = new nlobjSearchColumn('custbody_some_custom_fiels');
});
//! NOTE: Extending can still work in some cases, but don't assume it will.
_.extend(TransactionHistoryModel, {
name: 'TransactionHistory'
});
});
Best Practice: Add “after” listeners to backend functions and manipulate the data after the native function has finished.
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 liked this article, you'll LOVE our book on SuiteCommerce! Order the free SuiteCommerce book today, and we'll even pay for shipping!
Oracle NetSuite Alliance Partner & Commerce Partner
If you have general questions about SuiteCommerce or more specific 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!
We are a premium SuiteCommerce agency that creates powerful customer portals. Unlike our competitors, we have already solved your problems.
Tagged with Training