I have a RESTLet written in Suite Script 2.0. In this RESTLet, I want to use 'N/dataset' module to access Suite Analytics datasets. I am testing this with my demo NetSuite account and in my account I have Suite Analytics feature so there is no error while deploying the script on NetSuite as well when calling the its functionality from my C# code.
In below sample code, I have created a function to determine if dataset module is available or not however if NetSuite would gives "MODULE_DOES_NOT_EXIST" error while parsing the my .js file then I would not be able to deploy it.
Here I have a query what if I try to deploy this script in a NetSuite account that has not Suite Analytics, will NetSuite give an error saying "MODULE_DOES_NOT_EXIST" or it let me deployed the script and will only give the error when I will call some function related to Suite Analytics?
Requirement: Restlet has been working on many customer's account some of them has Suite Analytics (till now I have not used 'N/dataset' module, this is new requirement for the customer who has Suite Analytics). I do not want to keep two copy of Restlet (one for customer's without Suite Analytics and one for with Suite Analytics)
define
(
['N/record'
,'N/search'
,'N/runtime'
,'N/dataset'
,'N/query'
], mainFuncion
);
var dataset;
/********************** MAIN FUNCTION (Entry Point)**********************
function mainFuncion(pRecordMdule, pSearchModule, pRuntimeModule, pDatasetModule, pQueryModule)
{
dataset = pDatasetModule;
var isSuiteAnalyticsAvailable = checkIfSuiteAnalyticsAvilable();
}
//<--Suite Analytics - Dataset changes
function checkIfSuiteAnalyticsAvailable()
{
var isSuiteAnalyticsAvailable = false;
try
{
var isSuiteAnalyticsAvailable = false;
// Attempt to load all datasets
var dsList = dataset.list();
// If no error occurs in above line, SuiteAnalytics is available
isSuiteAnalyticsAvailable = true;
}
catch (ex)
{
// If an error occurs, it might indicate SuiteAnalytics is not available
isSuiteAnalyticsAvailable = false;
log.debug("SuiteAnalytics is not available in this NetSuite account.", e.message);
}
return isSuiteAnalyticsAvailable;
}
Try this, I think it will resolve your query
pRuntimeModule.isFeatureInEffect({ feature: "SUITEANALYTICSCONNECT" }); //SuiteAnalytics Connect
pRuntimeModule.isFeatureInEffect({ feature: "USR" }); //SuiteAnalytics Workbook
The following code is quickly typed and not tested but will definitely show you how to enable and disable scripts as well. I am not aware what type of script you are using but you would need to change userEventScript
to mapReduceScript
or whatever type of script you are using when updating the script deployment status.
You will also need the N/query
module
Hope this helps...
if (pRuntimeModule.isFeatureInEffect({ feature: "SUITEANALYTICSCONNECT" })) {
// Get the script and deploy Id's
const scriptIdQuery = _getScriptId('customscript_my_script_id');
const deployIdQuery = _getScriptDeploymentId('customdeploy_my_script_deploy_id')
// Get the ID of the script
pRecordMdule.load({
type: pRecordMdule.Type.SCRIPT_DEPLOYMENT,
id: scriptIdQuery
});
scriptDeploymentRecord.setValue({ fieldId: 'isdeployed', value: true });
scriptDeploymentRecord.save();
// Get the Id of the scripts deployment
pRecordMdule.load({
type: `userEventScript`,
id: deployIdQuery
});
scriptRecord.setValue({ fieldId: 'isinactive', value: false });
scriptRecord.save();
}
const _getScriptId = (scriptId) => {
const scriptIdQuery = `SELECT script.id
FROM script
WHERE scriptid = '${scriptId}'`;
let results = executeQuery(scriptIdQuery);
if (results[0]['id']) {
return results[0]['id'];
} else {
return false;
}
}
const _getScriptDeploymentId = (scriptInternalId) => {
const scriptDeploymentIdQuery = `SELECT primarykey
FROM scriptdeployment sd
INNER JOIN script
ON sd.script = script.id
WHERE script.id = '${scriptInternalId}'`
let results = executeQuery(scriptDeploymentIdQuery);
if (results[0]['primarykey']) {
return results[0]['primarykey'];
} else {
return false;
}
}