Articles in this section

Ignore specific errors in a postSubmit hook

This article explains how to use the postSubmit hook to ignore error messages in a flow.

Example: Customer import errors

This example syncs Shopify refunds with NetSuite.

360088415131-shopifyNS.png

Some imported customers have subsidiary value that should not be updated, and those records cause a specific error:

  "responseData": [
    {
      "statusCode": 422,
      "errors": [
          {
            "_userId": "5d93d813fe3c7b0ef52e58e8",
            "resolved": false,
            "code": "cant_modify_sub",
            "message": "Failed to save record because You cannot change the subsidiary of this entity because one or more transactions exist for this entity.",
            "source": "netsuite",
            "exportDataURI": null,
            "traceKey": "2178928902215"
          }
      ],
      "ignored": false,
      "id": "ExampleID",
      "_json": {ExampleJSON},
      "dataURI": "ExampledataURI"
    }
  ]

In such cases, you can use a conditional statement in a postSubmit hook to check for that specific error message and ignore it so that those errors do not generate notifications.

Add script to process error message (postSubmit script)

In the following postSubmit hook script, the highlighted conditional statement checks for the above error message and marks it as ignored:

function postSubmitNSCreateCustomerError(options) {
  let responseData = processPostSubmitErrors(options, ignoreCreateCustomerErrorFn)
  return responseData
}
function ignoreCreateCustomerErrorFn(options) {
  let toReturn = {ignored: options.ignored, betterMessage: options.message}
  //Check for specific error message inside "errors"    if (options.source === 'netsuite' &&       options.code === 'cant_modify_sub' &&      options.message.includes('Failed to save record because You cannot change the subsidiary of')) {   toReturn.ignored = true    toReturn.betterMessage = ''  
  }
  return toReturn
}
function processPostSubmitErrors(postSubmitOptions, myProcessErrorFunction) {
  postSubmitOptions.responseData.forEach(function(rd, i) {
    if (rd.statusCode !== 422) return
    let processedErrors = []
    rd.errors.forEach(function(re) {
      let errsHelper = []
      let parsedMessage = re.message
      try {
        parsedMessage = JSON.parse(re.message)
      } catch (ex) {}
      // Modify the following for your needs.
      if (typeof parsedMessage === 'object') {
        if (Array.isArray(parsedMessage)) {
          errsHelper = parsedMessage    
        } else if (Array.isArray(parsedMessage.errors)) {
          errsHelper = parsedMessage.errors
        } else {
          errsHelper.push({code: re.code, message: re.message})
        }
      } else {
        errsHelper.push({code: re.code, message: re.message})
      }
      errsHelper.forEach(function(e) {
        let processedErrorMessage = myProcessErrorFunction({
          code: e.code || re.code,
          message: e.message || re.message,
          source: re.source,
          ignored: re.ignored,
          preMapDataRecord: postSubmitOptions.preMapData[i]
        })
        processedErrors.push({
          code: e.code || re.code,
          message: processedErrorMessage.betterMessage || e.message || re.message,
          source: re.source,
          ignored: processedErrorMessage.ignored || re.ignored          
        })
      })
    })
    rd.errors = processedErrors
    let allErrorsIgnored = true
    for (let j = 0; j < processedErrors.length; j++) {
      if (!processedErrors[j].ignored) {
        allErrorsIgnored = false
        break;
      }
    }
    if (allErrorsIgnored) {
      rd.ignored = true
      rd.errors = []
      if (!rd._json) rd._json = {}
      rd._json.ignoredErrors = processedErrors
    }
  })
  return postSubmitOptions.responseData
}

Here is the preview of function output when you run the script inside the script editor:

postSubmitExample8.png

Testing the code

The following animation demonstrates how to test the code:

postSubmitExample9.gif
Was this article helpful?
1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.