Adding Listeners to SuiteCommerce Server/Backend Source Code

in , November 13th, 2024
person holding a computer mouse in a room

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.

Benefits of Updating SuiteCommerce Backend Code

Updating the backend code can have many benefits:

  1. Fast general code base (you don’t need to create and call your own service files)
  2. All data loaded at once
  3. Simpler extension
  4. More resistant to future SuiteCommerce updates

Adding Listeners: SuiteCommerce Backend Code Examples

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.

Author: Sam Gagliardi


Got stuck on a step in this article?

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!

FREE SuiteCommerce Book

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.


 
 

Want to keep learning?

Our team of NetSuite professionals has written articles on a wide variety of NetSuite topics, from SuiteCommerce tips, to recommended NetSuite solutions, to available support services, and more! 

Your cart