如何使用按钮启动预定脚本?

Ale*_*ndr 0 button netsuite suitescript

我想以执行计划脚本的形式创建一个按钮,但我尝试的任何方法都不起作用

我尝试过 addButton 方法来调用一个为预定脚本创建任务的函数,但它仅在表单加载时执行

/**
 *@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);
  }
});
Run Code Online (Sandbox Code Playgroud)

执行计划的脚本,但仅在表单加载中,然后按钮不执行任何操作,请帮助并感谢您的阅读

Ale*_*ndr 6

我最后成功了。它创建了 3 个脚本,一个调用客户端脚本的用户事件,该脚本使用为计划脚本创建任务的函数向 suitelet 发出 get 请求

这是用户事件:

/**
 *@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,
  };


});

Run Code Online (Sandbox Code Playgroud)

这是客户端脚本:

/**
 *@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
  };
});

Run Code Online (Sandbox Code Playgroud)

这是套房:

/**
 *@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
  };
});



Run Code Online (Sandbox Code Playgroud)

我希望这可以帮助另一个遇到同样问题的人。