SuiteScript Workflow Action-如何添加错误处理

SuiteScript Workflow Action-how to add error handling

我有一个工作流操作脚本,它应该在电子邮件正文中搜索字符串(该字符串用于文档编号——它存储在 ID 为 'custevent_case_creation' 的字段中)和return交易记录id。

脚本:

        /**
     *@NApiVersion 2.x
     *@NScriptType WorkflowActionScript
     * @param {Object} context
     */
    
    define(["N/search", "N/record"], function (search, record) {
      function onAction(context) {
        var recordObj = context.newRecord;
        var oc_number = recordObj.getValue({ fieldId: "custevent_case_creation" });
        var s = search
          .create({
            type: "salesorder",
            filters: [
              search.createFilter({
                name: "tranid",
                operator: search.Operator.IS,
                values: [oc_number],
              }),
            ],
            columns: ["internalid"],
          })
          .run()
          .getRange({
            start: 0,
            end: 1,
          });
        log.debug("result set", s[0].id);
    
        return s[0].id;
      }
    
      return {
        onAction: onAction,
      };
    });

当电子邮件中使用了有效的文档编号时,这会按预期工作。

但是,有两种情况不会出现这种情况:

  1. 原始电子邮件中没有引用文档编号(因此,字段“custevent_case_creation”将为空白)
  2. 引用的单据编号不正确,系统中没有与该单据编号的交易

我正在尝试添加某种形式的错误处理来处理这两种情况,但我找不到任何有效的方法。错误处理应该在这个脚本的什么地方? 应该是 if/else 语句吗?

到目前为止我已经尝试过:

我收到的错误消息是:

    org.mozilla.javascript.EcmaError: TypeError: Cannot read property "id" from undefined (/SuiteScripts/sdf_ignore/Workflow Action Lookup SO.js#41)

编辑: 工作代码

    /**
     *@NApiVersion 2.x
     *@NScriptType WorkflowActionScript
     * @param {Object} context
     */
    
    define(["N/search", "N/record"], function (search, record) {
      function onAction(context) {
        try {
          var recordObj = context.newRecord;
          var oc_number = recordObj.getValue({
            fieldId: "custevent_case_creation",
          });
          var s = search
            .create({
              type: "salesorder",
              filters: [
                search.createFilter({
                  name: "tranid",
                  operator: search.Operator.IS,
                  values: [oc_number],
                }),
              ],
              columns: ["internalid"],
            })
            .run()
            .getRange({
              start: 0,
              end: 1,
            });
          log.debug("result set", s[0].id);
    
          return s[0].id;
        } catch (error) {
          log.debug(
            error.name,
            "recordObjId: " +
              recordObj.id +
              ", oc_number:" +
              oc_number +
              ", message: " +
              error.message
          );
        }
      }
    
      return {
        onAction: onAction,
      };
    });

尝试用 try/catch 包装 onAction 函数的内容。有关 try/catch 的更多信息,请参见 here on W3Schools

try {
    //your working code for onAction function
    var recordObj = context.newRecord;
    var oc_number = recordObj.getValue({ fieldId: "custevent_case_creation" });
    var s = search.create({
        type: "salesorder",
        filters: [
          search.createFilter({
            name: "tranid",
            operator: search.Operator.IS,
            values: [oc_number]
          })
        ],
        columns: ["internalid"]
      })run().getRange({
        start: 0,
        end: 1,
      });
    log.debug("result set", s[0].id);

    return s[0].id;
} catch(e){
    log.debug(e.name,'recordObjId: '+ recordObj.id +', oc_number:'+ oc_number +', message: ' + e.message); //if e.name is empty try e.title
    //you can add additional steps here if desired, i.e. send an email, display an alert, etc.
}