Name

sn_cmdb_int_util.IREImportCommentParser

Description

No description available

Script

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

  parseImportSetResults: function(importSetId, importSetRun) {

      var grImpSet = new GlideRecord("sys_import_set");
      grImpSet.get(importSetId);
      var synchronous = grImpSet.getValue("mode") == "synchronous";

      var aggregate = {};
      var gr = new GlideRecord("sys_import_set_row");
      gr.addQuery("sys_import_set", importSetId);

      // if this is a synchronous import set then each run will add import set rows
      // to the same import set and we only want the rows created after this run
      // was created
      if (synchronous && importSetRun != null) {
          gr.addQuery("sys_created_on", ">=", importSetRun.getValue('sys_created_on'));
      }

      gr.query();
      while (gr.next()) {
          this.parseImportSetRow(gr, aggregate);
      }

      var executionIs = new GlideRecord("sn_cmdb_int_util_cmdb_integration_execution_import_set");
      executionIs.addQuery("import_set", importSetId);
      executionIs.query();
      if (!executionIs.next()) {
          return;
      }

      for (var ciClass in aggregate) {
          for (var operation in aggregate[ciClass]) {
              var operationGr = new GlideRecord("sn_cmdb_int_util_cmdb_integration_execution_audit");
              operationGr.initialize();
              operationGr.setValue("cmdb_integration_execution_import_set", executionIs.sys_id);
              operationGr.setValue("count", aggregate[ciClass][operation]);
              operationGr.setValue("operation", operation);
              operationGr.setValue("ci_class", ciClass);
              operationGr.insert();
          }
      }
      // Rollup IRE counts to execution
      new sn_cmdb_int_util.CMDBIntegrationExecutionRollup().updateIRECountsFromAudit(executionIs.cmdb_integration_execution.sys_id);

  },

  parseImportSetRow: function(importSetRow, aggregate) {
      var comment = importSetRow.getValue("sys_import_state_comment");
      if (gs.nil(comment))
          return;
      var index = comment.indexOf("{");
      if (index < 0)
          return;
      var json = comment.substring(index);
      var parsed = {};

      try {
          parsed = JSON.parse(json);

          var keys = Object.keys(parsed);
          var hasPartial = false;
          for (var i = 0; i < keys.length; i++) {
              var key = keys[i];
              if (!aggregate[key]) {
                  aggregate[key] = {};
              }
              var operations = Object.keys(parsed[key]);
              for (var j = 0; j < operations.length; j++) {
                  var operation = operations[j];
                  if (parsed[key][operation] > 0) {
                      if (operation == "partial") {
                          hasPartial = true;
                      }
                      if (!aggregate[key][operation]) {
                          aggregate[key][operation] = 0;
                      }
                      aggregate[key][operation] += parsed[key][operation];
                  }
              }
          }

          //If row is error, call row parser.
          var util = new sn_cmdb_int_util.CMDBIntegrationErrorParser();
          util.parseRow(importSetRow);

          if (hasPartial) {
              //If row has partial and verbose is enabled, call warning parser.
              util.parseRowForIREWarnings(importSetRow);
          }
      } catch (ex) {
          var errorMsg = "CMDB Integration Execution: Error parsing JSON from comment field. Exception: " + ex;
          gs.error(errorMsg);

          var executionIs = new GlideRecord("sn_cmdb_int_util_cmdb_integration_execution_import_set");
          executionIs.addQuery("import_set", importSetRow.sys_import_set.sys_id);
          executionIs.query();
          if (!executionIs.next()) {
              gs.info("Unable to find execution record to update state");
              return;
          }
          executionIs.setValue("state", "error");
          executionIs.setValue("most_recent_error", errorMsg);
          executionIs.update();

          new sn_cmdb_int_util.CMDBIntegrationErrorParser().createErrorRecord(importSetRow, errorMsg, 'IRE');
      }
  },

  type: 'IREImportCommentParser'
};

Sys ID

4fe1b5e673320010011e3e2dfef6a75b

Offical Documentation

Official Docs: