Reputation: 75
i want to create a button in a form that executes a scheduled script but nothing that i try works
I have tried the method addButton to call a function that create a task for the scheluded script but it only execute on form load
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(["N/task", "N/ui/serverWidget"], function(task, serverWidget) {
function beforeLoad(context) {
context.form.addButton({
id: "custpage_setTask",
label: "Execute scheduled",
functionName: executeScheduled()
});
}
return {
beforeLoad: beforeLoad
};
function executeScheduled() {
var scriptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: 'customscript_sdr_ss_product_shortage',
deploymentId: 'customdeployprodshortsearch'
});
var scriptTaskId = scriptTask.submit();
log.debug('scriptTaskId', scriptTaskId);
}
});
that execute the scheduled script but only in the form loading and then the button does not do anything, please help and thanks for reading
Upvotes: 0
Views: 4573
Reputation: 75
I managed to do it at the end. It was creating 3 scripts, a user event that calls a client script, that do a get request to a suitelet with the function that creates the task for the scheduled script
This is the User event:
/**
*@NApiVersion 2.x
*@NScriptType UserEventScript
*/
define(["N/task", "N/ui/serverWidget"], function(task, serverWidget) {
function beforeLoad(context) {
// internal id of the client script on file cabinet
context.form.clientScriptFileId = 2343;
context.form.addButton({
id: "custpage_setTask",
label: "Execute scheduled",
functionName: 'redirect'
});
}
return {
beforeLoad: beforeLoad,
};
});
This is the client script:
/**
*@NApiVersion 2.x
*@NScriptType ClientScript
*/
define(["N/url", "N/https"], function(url, https) {
function pageInit(context) {}
function redirect() {
var output = url.resolveScript({
scriptId: "customscript_sss_sl_startschedulescript",
deploymentId: "customdeploy_sss_sl_startschedulescript"
});
log.debug('url', output);
var response = https.get({
url: output
});
log.debug('response', response);
}
return {
pageInit: pageInit,
redirect: redirect
};
});
and this is the suitelet:
/**
*@NApiVersion 2.x
*@NScriptType Suitelet
*/
define(["N/task"], function(task) {
function onRequest(context) {
executeScheduled();
}
function executeScheduled() {
var scriptTask = task.create({
taskType: task.TaskType.SCHEDULED_SCRIPT,
scriptId: "customscript_sdr_ss_product_shortage",
deploymentId: "customdeployprodshortsearch"
});
var scriptTaskId = scriptTask.submit();
log.debug("scriptTaskId", scriptTaskId);
}
return {
onRequest: onRequest
};
});
I hope this helps another one with the same problem.
Upvotes: 6
Reputation: 2840
I believe you also need to export the button function.
return {
beforeLoad: beforeLoad,
executeScheduled:executeScheduled
};
You also need to change your button options
context.form.addButton({
id: "custpage_setTask",
label: "Execute scheduled",
functionName: executeScheduled
});
Upvotes: 0