Name

sn_cmdb_int_util.IntegrationHubFlowContextParser

Description

Parse integration hub flow context for errors

Script

var IntegrationHubFlowContextParser = Class.create();
IntegrationHubFlowContextParser.prototype = {
  initialize: function() {},

  parseFlowContext: function(flowContextId) {
      var grExecution = new GlideRecord('sn_cmdb_int_util_cmdb_integration_execution_import_set');
      var executionId = gs.getSession().getClientData('execution_context');
      grExecution.addQuery('cmdb_integration_execution', executionId);
      grExecution.orderByDesc('Created');
      grExecution.setLimit(1);
      grExecution.query();

      if (grExecution.next()) {
          var import_set_id = grExecution.getValue('import_set');
          var grImportSet = new GlideRecord('sys_import_set');
          grImportSet.addQuery('sys_id', import_set_id);
          grImportSet.query();
          if (grImportSet.next()) {
              var responseJSON = this._getResponseJSON(flowContextId);
              if (!responseJSON || gs.nil(responseJSON)) {
                  return null;
              }
              var statusCode = responseJSON.status_code.value;
              var category = "Connection";
              if ("401" == statusCode || "403" == statusCode) {
                  category = "Credentials";
              }
              var errorMessage = responseJSON.error_message.displayValue;
              gs.debug("errorMessage:" + errorMessage);
              var responseBody = responseJSON.response_body.displayValue;
              gs.debug("responseBody:" + responseBody);
              var errorParser = new CMDBIntegrationErrorParser();
              if (errorMessage)
                  errorParser.createImportErrorRecord(grImportSet, errorMessage, category);
              if (responseBody)
                  errorParser.createImportErrorRecord(grImportSet, responseBody, category);
              // set state to error as the import set state is not updated
              grExecution.state = 'error';
              grExecution.update();
          }
      }
  },

  _getResponseJSON: function(flowContextId) {
      var responseJSON = {};
      var flowRuntimeValueGr = new GlideRecord("sys_flow_runtime_value");
      flowRuntimeValueGr.addQuery("context", flowContextId);
      flowRuntimeValueGr.addQuery("type", "OUTPUT");
      flowRuntimeValueGr.query();
      try {
          if (flowRuntimeValueGr.next())
              responseJSON = JSON.parse(flowRuntimeValueGr.getValue("value"));
      } catch (ex) {
          gs.error(gs.getMessage("Error: parsing json message: {0} value: {1}", [ex, flowRuntimeValueGr.getValue("value") + '']));
          return null;
      }
      gs.debug("Output: \n" + JSON.stringify(responseJSON, null, 2));

      if (!responseJSON || gs.nil(responseJSON)) {
          return null;
      }
      if (!responseJSON.status_code) {
          gs.warn(gs.getMessage("No response status code returned from sys_flow_runtime_value.value"));
          return null;
      }
      var statusCode = responseJSON.status_code.value;
      gs.debug("responseJSON.status_code.value:" + responseJSON.status_code.value);
      if ("200" == statusCode) {
          return null;
      }

      return responseJSON;
  },

  type: 'IntegrationHubFlowContextParser'
};

Sys ID

91994e9673630010202627b751ab9e2c

Offical Documentation

Official Docs: