3

I am writing a script in SS2 and I ran into doc that stated that I should do the below. I have done this and the paths are right but it doesn't seem to see underscore as it comes back as undefined, when trying to use it. Any help with this would be great, thanks define( [ 'N/email' , 'N/runtime' , 'N/search' , '/SuiteScripts/af_scripts/underscore.js@1.8.3/underscore' ], function( email, runtime, search, _) { function onRequest( context ) {

        var request = context.request;

        var us = _;
    }

    return {
        onRequest: onRequest
    };
} );

jk121960
  • 843
  • 14
  • 45

3 Answers3

1

I have not see how to do it that way. But the way I found that works is like this:

/**
* @NApiVersion 2.x
* @NScriptType usereventscript
*/
require.config({
  paths:{
    "coolthing":"/SuiteScripts/myFavoriteJsLibrary"
  }
});
define(['coolthing'],function (coolthing){
  return {
    beforeLoad:function beforeLoad(ctx){
      coolthing.times(2,function(){
        log.debug('log','log');
      });
    }
  };
});
w3bguy
  • 2,215
  • 1
  • 19
  • 34
  • 1
    Thanks, close enough. I also found the reference to using the shim with non-AMD modules, thanks that helps – jk121960 Apr 15 '17 at 13:47
1

I put underscore-min.js in the same folder as my Suitescripts.

Then I created a config file named 'underscoreConfig.json' in the same folder with the following contents:

{
  "paths":{
    "underscore": "./underscore-min"
  }
}

Then I add the following to my scripts:

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope SameAccount
 * @NAmdConfig ./underscoreConfig.json
 */
define(['underscore', 'N/record', 'N/search'],
/**
 * @param {underscore} underscore
 * @param {record} record
 * @param {search} search
 */
function(_, record, search) {

Now I am able to call Underscore functions in my scripts.

E.g.

var segmentObj = _.findWhere(segmentFields, {id: segmentId});
1

In my case, I used lodash to add HTML file contents to my suitelet When you define your modules you can insert the lodash library at the end just like

define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],

but at function you should not insert anything just like

function(file, record, search, serverWidget) {

Here is a sample of code to load a file and get his contents using lodash

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 * @NModuleScope SameAccount
 */
define(['N/file', 'N/record', 'N/search', 'N/ui/serverWidget','./lodash.js'],
/**
 * @param {file} file
 * @param {record} record
 * @param {search} search
 * @param {serverWidget} serverWidget
 */
function(file, record, search, serverWidget) {

    /**
     * Definition of the Suitelet script trigger point.
     *
     * @param {Object} context
     * @param {ServerRequest} context.request - Encapsulation of the incoming request
     * @param {ServerResponse} context.response - Encapsulation of the Suitelet response
     * @Since 2015.2
     */
    function onRequest(context) {

        var templateFile = file.load({
            id: 'filePath'
        });
        //for example.
        var compiled = _.template(templateFile.getContents());

    }

    return {
        onRequest: onRequest
    };

});

Note:

I inserted the file to the same location as my suitelet in the file cabinet that why I used this relative path (./lodash.js), use the full path if the suitelet is not at the same file.