Create your custom module file and upload it to the File Cabinet. For example, create a JavaScript file containing the following code and name it math.js, then save it in the SuiteScripts/Example folder in the File Cabinet.
// math.js
define([], function() {
function add(a, b) {
return a + b;
}
function subtract(a, b) {
return a – b;
}
function multiply(a, b) {
return a * b;
}
function divide(a, b) {
if (b === 0) {
throw new Error(‘Division by zero’);
}
return a / b;
}
return {
add: add,
subtract: subtract,
multiply: multiply,
divide: divide
};
});
To load the custom module by name, specify the module file name (alias) and its path by configuring the paths parameter in a JSON file (for example, myconfig.json).
.
{
“paths”: {
“math”: “/SuiteScripts/Example/math.js”
}
“shim”:{
“math”: {
“exports”: “myMath”
}
}
}
Load the custom module in your SuiteScript 2.x script by including the @NAmdConfig JSDoc tag with your .json file name and passing the custom module name to the define Object.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NAmdConfig ./myconfig.json
*/
define([‘math’], function (math) {
return {
beforeLoad: function beforeLoad() {
log.debug({
title: ‘test’,
details: myMath.add(1,2)
});
}
}
});