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

发布于 2025-01-13 04:59:38 字数 3177 浏览 1 评论 0原文

我有一个工作流程操作脚本,该脚本应该在电子邮件正文中搜索字符串(该字符串表示文档编号,该字符串存储在 id 为“custevent_case_creation”的字段中)并返回事务记录 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 语句吗?

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

  • 添加 if{s.length>0);
  • 在工作流本身中添加一个条件,以便在 custevent_case_creation 字段为空时不会发生工作流操作脚本中的自定义操作 -工作流程操作 UI

我收到的错误消息是:

    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,
      };
    });

I have a workflow action script that is supposed to search for a string in an email message body (the string being for the document number--this is stored in a field with the id 'custevent_case_creation') and return the transaction record id.

The script:

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

This works as expected when there is a valid document number used in the email message.

However, there are two scenarios where that won't be the case:

  1. there is no document number referenced in the original email (and therefore, the field "custevent_case_creation" will be blank)
  2. the document number referenced is incorrect and there is no transaction with that document number in the system

I am trying to add some form of error handling to deal with these two scenarios though I can't find anything that works. Where should the error handling be in this script?
Should it be an if/else statement?

So far I have tried:

  • adding if{s.length>0);
  • adding a condition in the workflow itself so that the custom action from the workflow action script doesn't occur if the field for custevent_case_creation is blank
    -workflow action UI

The error message I am getting is:

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

EDIT:
The working code

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈场末日恋爱 2025-01-20 04:59:38

尝试用 try/catch 包装 onAction 函数的内容。有关 try/catch 的更多信息可以在 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.
}

Try wrapping the contents of you onAction function with try/catch. More info on try/catch can be found 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.
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文