Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented: Restlet to update and run suitescript from external syst… #190

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/FileCabinet/SuiteScripts/Restlet/HC_RL_RunSuiteScript.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define(['N/search', 'N/task'],
(search, task) => {
const post = (requestParams) => {
var returnMessage = '';
var scriptName = requestParams.name;
var scriptType = requestParams.scriptType;

if (scriptType === 'SCHEDULED') {
// Search for the Script ID using the Script Name
var scheduledScriptId = search.create({
type: search.Type.SCHEDULED_SCRIPT,
filters: [['name', 'is', scriptName]],
columns: ['scriptid']
}).run().getRange({ start: 0, end: 1 }).map(function (result) {
return result.getValue('scriptid');
})[0];

var scheduledDeploymentId = search.create({
type: 'scriptdeployment',
filters: [['script.scriptid', 'is', scheduledScriptId], 'AND', ['status', 'is', 'NOTSCHEDULED']],
columns: ['scriptid']
}).run().getRange({ start: 0, end: 1 }).map(function (result) {
return result.getValue('scriptid');
})[0];

if (scheduledScriptId && scheduledDeploymentId) {
try {
// Create a scheduled script task
var scheduledScriptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: scheduledScriptId,
deploymentId: scheduledDeploymentId
});

// Submit the task
var taskId = scheduledScriptTask.submit();
log.debug('Schedule Script Rescheduled with task ID: ' + taskId);
returnMessage = 'Success: Task successfully submitted!';
} catch (e) {
log.error('Error in Scheduled Script', e.toString());
returnMessage = 'Error: ' + e.toString();
}
}
}
if (scriptType === 'MAP_REDUCE') {
// Search for the Script ID using the Script Name
var scriptId = search.create({
type: search.Type.MAP_REDUCE_SCRIPT,
filters: [['name', 'is', scriptName]],
columns: ['scriptid']
}).run().getRange({ start: 0, end: 1 }).map(function (result) {
return result.getValue('scriptid');
})[0];

var scriptDeploymentId = search.create({
type: 'scriptdeployment',
filters: [['script.scriptid', 'is', scriptId], 'AND', ['status', 'is', 'NOTSCHEDULED']],
columns: ['scriptid']
}).run().getRange({ start: 0, end: 1 }).map(function (result) {
return result.getValue('scriptid');
})[0];

if (scriptId && scriptDeploymentId) {
try {
// Create a scheduled script task
var scheduledScriptTask = task.create({
taskType: task.TaskType.MAP_REDUCE,
scriptId: scriptId,
deploymentId: scriptDeploymentId
});

// Submit the task
var taskId = scheduledScriptTask.submit();
log.debug('Map/Reduce Script Rescheduled with task ID: ' + taskId);
returnMessage = 'Success: Task successfully submitted!';
} catch (e) {
log.error('Error in Scheduled Script', e.toString());
returnMessage = 'Error: ' + e.toString();
}
}
}
return JSON.stringify(returnMessage);
}
return {post}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define(['N/record', 'N/search'],
( record, search) => {
const post = (requestBody) => {
var scriptName = requestBody.name;
var paused = requestBody.paused;

var scriptInternalId = search.create({
type: 'scriptdeployment',
filters: [['title', 'is', scriptName], 'AND', ['status', 'noneof', 'NOTSCHEDULED']],
columns: ['internalid']
}).run().getRange({ start: 0, end: 1 }).map(function (result) {
return result.getValue('internalid');
})[0];

if (scriptInternalId && paused) {
if (paused === 'Y') {
record.submitFields({
type: record.Type.SCRIPT_DEPLOYMENT,
id: scriptInternalId,
values: {
status: 'TESTING'
}
});
}
if (paused === 'N') {
record.submitFields({
type: record.Type.SCRIPT_DEPLOYMENT,
id: scriptInternalId,
values: {
status: 'SCHEDULED'
}
});
}

}
return {
status: 'success',
message: 'Successfully updated the status.',
requestBody: requestBody
};
}
return {post}
});
22 changes: 22 additions & 0 deletions src/Objects/Restlet/customscript_restlet_runscript.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<restlet scriptid="customscript_restlet_runscript">
<description></description>
<isinactive>F</isinactive>
<name>HC_RL_RunSuiteScript</name>
<notifyadmins>F</notifyadmins>
<notifyemails></notifyemails>
<notifyowner>T</notifyowner>
<notifyuser>F</notifyuser>
<scriptfile>[/SuiteScripts/Restlet/HC_RL_RunSuiteScript.js]</scriptfile>
<scriptdeployments>
<scriptdeployment scriptid="customdeploy_restlet_runscript">
<allemployees>F</allemployees>
<allpartners>F</allpartners>
<allroles>T</allroles>
<audslctrole></audslctrole>
<isdeployed>T</isdeployed>
<loglevel>DEBUG</loglevel>
<status>RELEASED</status>
<title>HC_RL_RunSuiteScript</title>
</scriptdeployment>
</scriptdeployments>
</restlet>
22 changes: 22 additions & 0 deletions src/Objects/Restlet/customscript_restlet_updatescript.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<restlet scriptid="customscript_restlet_updatescript">
<description></description>
<isinactive>F</isinactive>
<name>HC_RL_UpdateSuiteScriptStatus</name>
<notifyadmins>F</notifyadmins>
<notifyemails></notifyemails>
<notifyowner>T</notifyowner>
<notifyuser>F</notifyuser>
<scriptfile>[/SuiteScripts/Restlet/HC_RL_UpdateSuiteScriptStatus.js]</scriptfile>
<scriptdeployments>
<scriptdeployment scriptid="customdeploy_restlet_updatescript">
<allemployees>F</allemployees>
<allpartners>F</allpartners>
<allroles>T</allroles>
<audslctrole></audslctrole>
<isdeployed>T</isdeployed>
<loglevel>DEBUG</loglevel>
<status>RELEASED</status>
<title>HC_RL_UpdateSuiteScriptStatus</title>
</scriptdeployment>
</scriptdeployments>
</restlet>
6 changes: 6 additions & 0 deletions src/deploy.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@
<path>~/FileCabinet/SuiteScripts/POSReturn/HC_MR_ExportedPOSReturnCSV.js</path>
<path>~/FileCabinet/SuiteScripts/SalesOrder/HC_MR_CreateCustomerDeposit.js</path>
<path>~/FileCabinet/SuiteScripts/SalesOrder/HC_MR_ExportedCustomerDepositCSV.js</path>

<path>~/FileCabinet/SuiteScripts/Restlet/HC_RL_UpdateSuiteScriptStatus.js</path>
<path>~/FileCabinet/SuiteScripts/Restlet/HC_RL_RunSuiteScript.js</path>
</files>
<objects>
<!--Custom Record-->
Expand Down Expand Up @@ -180,6 +183,9 @@
<path>~/Objects/POSReturn/customscript_exp_pos_return.xml</path>
<path>~/Objects/SalesOrder/customscript_hc_mr_createcustomerdeposit.xml</path>

<path>~/Objects/Restlet/customscript_restlet_updatescript.xml</path>
<path>~/Objects/Restlet/customscript_restlet_runscript.xml</path>

<!--Saved Search-->
<path>~/Objects/CashSale/customsearch_hc_export_cashsales.xml</path>
<path>~/Objects/Customer/customsearch_hc_export_customer.xml</path>
Expand Down
1 change: 1 addition & 0 deletions src/manifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
<feature required="false">CUSTOMTRANSACTIONS</feature>
<feature required="false">RECEIVABLES</feature>
<feature required="false">PAYABLES</feature>
<feature required="false">CRM</feature>
</features>
</dependencies>
</manifest>