You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NetSuite Suitelet按钮报错:executeScheduled函数未定义

NetSuite Suitelet按钮触发定时脚本报错:executeScheduled未定义

错误原因

你定义的executeScheduled是服务器端函数,仅在NetSuite服务器运行环境中存在,不会暴露到浏览器的客户端全局作用域。而Suitelet表单按钮的functionName参数要求的是客户端可访问的全局函数,因此点击按钮时浏览器找不到该函数,抛出ReferenceError。

修复方案

以下两种方案均可解决问题,根据需求选择:

方案一:客户端脚本发起POST请求(无页面刷新)

通过在表单中注入客户端脚本,按钮点击时向当前Suitelet发送POST请求,触发服务器端执行定时脚本:

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(["N/task", "N/ui/serverWidget"], function(task, serverWidget) {  
 function onRequest(context) {
  if(context.request.method === 'GET'){
   var form = serverWidget.createForm({
    title: 'Discrepancy Emailer Button'
   });
   // 添加按钮,指定客户端触发函数
   form.addButton({
     id: 'custombutton',
     label: 'Send Emails',
     functionName: 'clientTriggerScheduled'
   });
   // 注入inline客户端脚本到表单
   form.addField({
      id: 'custpage_inline_script',
      type: serverWidget.FieldType.INLINEHTML,
      label: 'Inline Script'
   }).defaultValue = `
     <script>
       function clientTriggerScheduled() {
         fetch(window.location.href, {
           method: 'POST',
           headers: {'Content-Type': 'application/x-www-form-urlencoded'},
           body: 'action=run_scheduled'
         })
         .then(res => res.text())
         .then(data => alert('定时脚本已提交,任务ID:' + data))
         .catch(err => alert('执行失败:' + err.message));
       }
     </script>
   `;
   context.response.writePage(form);
  } else if(context.request.method === 'POST'){
   // 处理POST请求,执行定时脚本
   if(context.request.parameters.action === 'run_scheduled'){
    const scriptTask = task.create({
     taskType: task.TaskType.SCHEDULED_SCRIPT,
     scriptId: "customscriptXXXX",
     deploymentId: "customdeploy1"
    });
    const taskId = scriptTask.submit();
    log.debug("Scheduled Script Task ID", taskId);
    context.response.write(taskId);
   }
  }
 }

 return { onRequest: onRequest };
});

方案二:表单提交触发服务器端执行(简单直接)

将按钮改为表单提交按钮,通过POST请求触发服务器端逻辑,无需额外客户端脚本:

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */
define(["N/task", "N/ui/serverWidget"], function(task, serverWidget) {  
 function onRequest(context) {
  if(context.request.method === 'GET'){
   var form = serverWidget.createForm({
    title: 'Discrepancy Emailer Button'
   });
   // 添加隐藏字段标识执行动作
   form.addField({
      id: 'custpage_action',
      type: serverWidget.FieldType.TEXT,
      label: 'Action'
   }).defaultValue = 'run_scheduled';
   // 添加提交按钮
   form.addSubmitButton({ label: 'Send Emails' });
   context.response.writePage(form);
  } else if(context.request.method === 'POST'){
   if(context.request.parameters.custpage_action === 'run_scheduled'){
    const scriptTask = task.create({
     taskType: task.TaskType.SCHEDULED_SCRIPT,
     scriptId: "customscriptXXXX",
     deploymentId: "customdeploy1"
    });
    const taskId = scriptTask.submit();
    log.debug("Scheduled Script Task ID", taskId);
    // 返回成功提示并提供返回链接
    context.response.write(`
      <html>
        <body>
          <p>定时脚本已提交,任务ID:${taskId}</p>
          <a href="${context.request.url}">返回按钮页面</a>
        </body>
      </html>
    `);
   }
  }
 }

 return { onRequest: onRequest };
});

内容的提问来源于stack exchange,提问作者Lem8820

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 08:09:58