title: SuiteScript Workflow Action-how to add error handling
body: <p>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.</p>
  <p>The script:</p>
  <pre class="lang-js prettyprint-override"><code>        /**
       *@NApiVersion 2.x
       *@NScriptType WorkflowActionScript
       * @param {Object} context
       */
      
      define([&quot;N/search&quot;, &quot;N/record&quot;], function (search, record) {
        function onAction(context) {
          var recordObj = context.newRecord;
          var oc_number = recordObj.getValue({ fieldId: &quot;custevent_case_creation&quot; });
          var s = search
            .create({
              type: &quot;salesorder&quot;,
              filters: [
                search.createFilter({
                  name: &quot;tranid&quot;,
                  operator: search.Operator.IS,
                  values: [oc_number],
                }),
              ],
              columns: [&quot;internalid&quot;],
            })
            .run()
            .getRange({
              start: 0,
              end: 1,
            });
          log.debug(&quot;result set&quot;, s[0].id);
      
          return s[0].id;
        }
      
        return {
          onAction: onAction,
        };
      });
  
  </code></pre>
  <p>This works as expected when there is a valid document number used in the email message.</p>
  <p>However, there are two scenarios where that won't be the case:</p>
  <ol>
  <li>there is no document number referenced in the original email (and therefore, the field &quot;custevent_case_creation&quot; will be blank)</li>
  <li>the document number referenced is incorrect and there is no transaction with that document number in the system</li>
  </ol>
  <p>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?</p>
  <p>So far I have tried:</p>
  <ul>
  <li>adding if{s.length&gt;0);</li>
  <li>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
  -<a href="https://i.stack.imgur.com/IAPP8.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IAPP8.png" alt="workflow action UI" /></a></li>
  </ul>
  <p>The error message I am getting is:</p>
  <pre class="lang-js prettyprint-override"><code>    org.mozilla.javascript.EcmaError: TypeError: Cannot read property &quot;id&quot; from undefined (/SuiteScripts/sdf_ignore/Workflow Action Lookup SO.js#41)
  </code></pre>
  <p>EDIT:
  The working code</p>
  <pre class="lang-js prettyprint-override"><code>    /**
       *@NApiVersion 2.x
       *@NScriptType WorkflowActionScript
       * @param {Object} context
       */
      
      define([&quot;N/search&quot;, &quot;N/record&quot;], function (search, record) {
        function onAction(context) {
          try {
            var recordObj = context.newRecord;
            var oc_number = recordObj.getValue({
              fieldId: &quot;custevent_case_creation&quot;,
            });
            var s = search
              .create({
                type: &quot;salesorder&quot;,
                filters: [
                  search.createFilter({
                    name: &quot;tranid&quot;,
                    operator: search.Operator.IS,
                    values: [oc_number],
                  }),
                ],
                columns: [&quot;internalid&quot;],
              })
              .run()
              .getRange({
                start: 0,
                end: 1,
              });
            log.debug(&quot;result set&quot;, s[0].id);
      
            return s[0].id;
          } catch (error) {
            log.debug(
              error.name,
              &quot;recordObjId: &quot; +
                recordObj.id +
                &quot;, oc_number:&quot; +
                oc_number +
                &quot;, message: &quot; +
                error.message
            );
          }
        }
      
        return {
          onAction: onAction,
        };
      });
  </code></pre>