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

NetSuite Suitelet生成PDF文件名遇意外错误(沙盒正常生产报错)

Netsuite Suitelet生产环境PDF生成报错排查

我有一个Suitelet脚本,用于根据采购订单的单据号生成PDF文件名。该脚本在沙盒环境运行正常,但部署到生产环境后出现以下错误:

{
    "type": "error.SuiteScriptError",
    "name": "UNEXPECTED_ERROR",
    "message": "An unexpected SuiteScript error has occurred",
    "stack": ["Error\n at Object.onRequest (/SuiteScripts/fc_sl_getdocnum_pdfname.js:21:28)"],
    "cause": {
        "type": "internal error",
        "code": "UNEXPECTED_ERROR",
        "details": "An unexpected SuiteScript error has occurred",
        "userEvent": null,
        "stackTrace": ["Error\n at Object.onRequest (/SuiteScripts/fc_sl_getdocnum_pdfname.js:21:28)"],
        "notifyOff": false
    },
    "id": "499c90df-3134-49ca-8f2b-c675360815d6-2d323032332e30312e3230",
    "notifyOff": false,
    "userFacing": true
}

以下是我参考博客内容并添加交易状态过滤后的脚本:

添加打印按钮的User Event脚本

define(['N/url','N/record'], function(url) {
  function beforeLoad(context) {
    if (context.type === context.UserEventType.VIEW) {
      const newRec = context.newRecord;
      const recStatus = newRec.getValue({ fieldId : 'custbody_fc_trans_approval_status'});
      
    //log.debug('status', recStatus);
      if (recStatus == 3) {
          try {
            const uiForm = context.form;
            const slUrl = url.resolveScript({
              scriptId: 'customscript_fc_sl_getdocnum_pdfname',
              deploymentId: 'customdeploy_fc_getdocnum_pdfname',
              params: {
                id: newRec.id
              }
            });

            uiForm.addButton({ id: 'custpage_ea_alt_print', label: 'Print', functionName: 'window.open("' + slUrl + '","_self")' });}
            catch (err) {
            log.error('err:' + newRec.id, err);}
            }
        }
    }
    return {
    beforeLoad: beforeLoad
  };
});

生成PDF并报错的Suitelet脚本

define(['N/render', 'N/search'],
  function(render, search) {
    function onRequest(context) {
      const req = context.request;
      const params = req.parameters;
      if (req.method === 'GET') {
        const tranLup = search.lookupFields({ type: 'transaction', id: params.id, columns: ['tranid', 'type'] });
        let pdfFile = null;
        if (tranLup.type[0].value === 'WorkOrd') {
          pdfFile = render.bom({ entityId: Number(params.id) });
        } else {
          pdfFile = render.transaction({ entityId: Number(params.id) });
        }
        pdfFile.name = tranLup.tranid + '.pdf';
        return context.response.writeFile({ file: pdfFile });
      }
    }
    return {
      onRequest: onRequest
    };
  }
);

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 20:35:27