Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a custom library module in SuitScript 2.0?

I am trying to create a custom profiler library to load into my SuiteScript 2.0 scripts. Its path is /SuiteScripts/profiler.js and my Suitelet is in /SuiteScripts/suitelet.js

Here how I am trying to load it into my suitelet:

define(['N/ui/serverWidget', 'N/log', 'N/file', './profiler'],
  function(serverWidget, log, file, profiler) {
    function onRequest(context) {

      log.debug('profiler', profiler) // --> logs undefined

I've tried two approaches in profiler.js:

Approach #1:

function Profiler() {
  var profileInstance = {};
  // add methods to profileInstance
  return profileInstance
}

Approach #2:

require([],
 function() {
   function profiler() {
     var profilerInstance = {};
     // add methods to profilerInstance
     return profilerInstance;
   }
return {
  profiler: profiler
 };
});

In Approach #2 I also added a comment indicating the SuiteScript version and require.config:

/**
 *@NApiVersion 2.x
 */
require.config({
 shim: {
  'profiler': {
    exports: 'profiler'
  }
},
 paths: {
  profiler: '/SuiteScripts/Buy Request/profiler.js'
 }
});

What am I doing wrong?

like image 778
Eric Broberg Avatar asked Feb 07 '23 20:02

Eric Broberg


1 Answers

You need to use define instead of require to build your custom modules. Just change your Approach #2 to call define instead of require, and you should be good to go.

I am not sure whether require.config works with SuiteScript or not. You shouldn't need it specifically for this use case.

like image 67
erictgrubaugh Avatar answered Feb 11 '23 01:02

erictgrubaugh