While we typically recommend using SuiteScript directly in your site’s code, it is also possible to use that code call Suitelets. They execute outside of the context of the web store code, which can be a benefit when working with ‘agnostic’ scripts that request and return data regardless of how it’s called, but it also means you lose access to some utility functions and APIs. However, as you’re calling it from your site’s source code, you can pass in any data you might need as parameters to the request URL.

They can be useful in situations where you have an existing script that you want to run, but you don’t want to rewrite/duplicate it in your site’s source code. They can also be a way to integrate SuiteScript 2.0 scripts into a site that cannot currently run them (ie older versions of SuiteCommerce Advanced).

The sample code in this tutorial uses a SuiteScript 2.0 Suitelet integrated into a site’s source code using SuiteScript 1.0 and service controllers. This is fine for newer versions, but versions older than Vinson (2016.2) will not be able to run it as-is.

General Approach

Generally speaking, instead of running all of your SuiteScript in your backend model, you instead call a Suitelet. In SuiteScript 1.0, this uses nlapiResolveURL().

For example, your backend model might look like this:

define('ExampleSuitelet.Model'
, [
    'SC.Model'
  ]
, function
  (
    SCModel
  )
{

  'use strict';

  return SCModel.extend({
    name: 'ExampleSuitelet'

  , get: function () {
      var stSuiteletUrl = nlapiResolveURL('SUITELET', 'customscript_example_suitelet','customdeploy_example_suitelet', true);
      stSuiteletUrl = stSuiteletUrl + '&customer=' + nlapiGetUser();

      var headers = new Array();
      headers['Content-Type'] = 'application/json';
      headers['User-Agent-x'] = 'SuiteScript-Call';

      var response = nlapiRequestURL(stSuiteletUrl, null, headers, 'GET');

      return response.getBody();
    }
  })
});

If you’re interested in Suitelets in general, you can read more in SuiteAnswers.

Deploy Example Code

In this example, the Suitelet will perform a search on user records for the current user and return various fields of data available in their record. We’re then just going to send it back to the frontend so it can be displayed when a user visits a particular URL path.

For brevity, a full example implementation is available in /samples/2017-2-kilimanjaro/ExampleSuitelet@1.0.0 in the GitHub repo. It’s written for the Kilimanjaro (2017.2) release of SuiteCommerce Advanced, but it is compatible with a number of versions and can be adapted for any version released after Mont Blanc (2016.1). Make sure you put it in your source code, update your disto.json to include it in modules, ssp-libraries, and myaccount.js, and deploy it up to your test site. If you’re using an extension, eg for SuiteCommerce, then you can adapt the code to fit your site.

After deploying the code, you’ll also need to upload a copy of the Suitelet stored in the example code (ADD ME TO THE BACKEND/ExampleSuitelet.js). First create a script record for it by going to Customization > Scripting > Scripts > New — for the purposes of this tutorial I am calling it _example_suitelet. Then, secondly, you will to deploy the script by clicking the Deploy Script button — again, I am calling this deployment record _example_suitelet.

When that’s done, log in to your site and visit #examplesuitelet in the my account section. You should see the data for your current user displayed.