Name

sn_grc.GRCExceptionUtilsBase

Description

No description available

Script

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

  /**
  Creates exception with given exception object
  Following properties to be set to create exception object sucessfully
  exception.table(Mandatory) - Table to which this excpetion belongs to 
  exception.sourceRecordId(Mandatory) - Record to which this excpetion belongs to 
  exception.fieldName - Field to which this exception belongs to 
  exception.exceptionCategory(Mandatory) - Exception category value
  exception.exceptionMessage(Mandatory) - Exception to log
  exception.allowMultipleExceptions - Mark this as true to allow multiple excpetions for same table and source reocrd. Mark this as false to always have a single exception for table and source record
  exception.application - Fill this property with Application SysId (sys_scope). so that we can tag to which application this exception belongs to
  **/
  
  deleteInvalidException: function() {
      return this._deleteInvalidException();
  },
  
  createException: function(exception) {
      return this._createException(exception);
  },

  updateMatchingExceptions: function(table, sourceRecordId, fieldName, state) {
      this._updateMatchingExceptions(table, sourceRecordId, fieldName, state);
  },

  updateExceptionRecordState: function(exceptionId, state) {
      this._updateExceptionRecordState(exceptionId, state);
  },

  //Retrieves latest exception if there are multiple exceptions matching the criteria
  getLatestException: function(table, sourceRecordId, fieldName, state) {
      return this._getLatestException(table, sourceRecordId, fieldName, state);
  },

  getException: function(exceptionId) {
      return this._getException(exceptionId);
  },
  
  //Delete exception records for which reference table or reference source already deleted. 
  _deleteInvalidException: function() {
      var exceptionRecord = new GlideRecord('sn_grc_exception');
  	var qc = exceptionRecord.addQuery('table','');
  	qc.addOrCondition('source_record','');
  	exceptionRecord.query();
  	exceptionRecord.deleteMultiple();
  	
  	exceptionRecord.initialize();
  	exceptionRecord.query();
  	while(exceptionRecord.next()){
  		var gr = new GlideRecord(exceptionRecord.getValue('table'));
  		if(gr.isValid()){
  			gr.get(exceptionRecord.getValue('source_record'));
  			if(!gr.isValidRecord()){
  				exceptionRecord.deleteRecord();
  			}
  		} else{
  			exceptionRecord.deleteRecord();
  		}
  	}	
  },

  _createException: function(exception) {
      var result = {};
      result.exceptionId = '';
      this._validateExceptionRequest(exception, result);

      if (!gs.nil(result.error))
          return result;

      var exceptionCategoryRecord = this._getExceptionCategory(exception.exceptionCategory);

      if (gs.nil(exceptionCategoryRecord)) {
          result.error = gs.getMessage('No Exception category record found for {} category given in the request.', exception.exceptionCategory);
          result.created = false;
          return result;
      }

      var exceptionRecord = this._getExceptionGlideRecord(exception.table, exception.sourceRecordId);
      if (!gs.nil(exception.fieldName)) {
          exceptionRecord.addQuery('field_name', exception.fieldName);
      }
      if (!gs.nil(exception.allowMultipleExceptions) && exception.allowMultipleExceptions) {
          exceptionRecord.addQuery('description', exception.exceptionMessage);
      }
      exceptionRecord.query();
      var exceptionId;
      if (exceptionRecord.next()) {
          this._setValuesForExceptionRecord(exception, exceptionRecord, exceptionCategoryRecord);
          exceptionId = exceptionRecord.update();
      } else {
          exceptionRecord = new GlideRecord('sn_grc_exception');
          exceptionRecord.setValue('table', exception.table);
          exceptionRecord.setValue('source_record', exception.sourceRecordId);
          this._setValuesForExceptionRecord(exception, exceptionRecord, exceptionCategoryRecord);

          exceptionId = exceptionRecord.insert();
      }
      if (!gs.nil(exceptionId)) {
          result.exceptionId = exceptionId;
      }
      return result;
  },

  _setValuesForExceptionRecord: function(exception, exceptionRecord, exceptionCategoryRecord) {
      if (!gs.nil(exception.fieldName))
          exceptionRecord.setValue('field_name', exception.fieldName);
      exceptionRecord.setValue('exception_category', exceptionCategoryRecord.getUniqueValue());
      exceptionRecord.setValue('state', '1');
      exceptionRecord.setValue('description', exception.exceptionMessage);
      if (!gs.nil(exception.application))
          exceptionRecord.setValue('application', exception.application);
  },

  _validateExceptionRequest: function(exception, result) {
      if (gs.nil(exception.table)) {
          result.error = gs.getMessage('table is missing in the request.');
          return result;
      }

      if (gs.nil(exception.sourceRecordId)) {
          result.error = gs.getMessage('sourceRecordId is missing in the request.');
          return result;
      }

      if (gs.nil(exception.exceptionCategory)) {
          result.error = gs.getMessage('exceptionCategory is missing in the request.');
          return result;
      }

      if (gs.nil(exception.exceptionMessage)) {
          result.error = gs.getMessage('exceptionMessage is missing in the request.');
          return result;
      }
      return result;
  },

  _updateMatchingExceptions: function(table, sourceRecordId, fieldName, state) {
      var exceptionRecord = this._getExceptionGlideRecord(table, sourceRecordId);
      if (!gs.nil(fieldName)) {
          exceptionRecord.addQuery('field_name', fieldName);
      }
      exceptionRecord.setValue('state', state);
      exceptionRecord.updateMultiple();
  },

  _updateExceptionRecordState: function(exceptionId, state) {
      var exceptionRecord = new GlideRecord('sn_grc_exception');
      if (exceptionRecord.get(exceptionId)) {
          exceptionRecord.setValue('state', state);
          exceptionRecord.update();
      }
  },

  _getException: function(exceptionId) {
      var exceptionRecord = new GlideRecord('sn_grc_exception');
      if (exceptionRecord.get(exceptionId)) {
          return exceptionRecord;
      }
      return null;
  },

  _getLatestException: function(table, sourceRecordId, fieldName, state) {
      var exceptionRecord = this._getExceptionGlideRecord(table, sourceRecordId);
      if (!gs.nil(fieldName)) {
          exceptionRecord.addQuery('field_name', fieldName);
      }
      if (!gs.nil(state)) {
          exceptionRecord.addQuery('state', state);
      }
      exceptionRecord.orderBy('sys_created_on');
      exceptionRecord.query();
      if (exceptionRecord.next())
          return exceptionRecord;
      return null;
  },

  _getExceptionCategory: function(exceptionCategory) {
      var exceptionCategoryRecord = new GlideRecord('sn_grc_exception_category');
      exceptionCategoryRecord.addQuery('category', exceptionCategory);
      exceptionCategoryRecord.query();
      if (exceptionCategoryRecord.next())
          return exceptionCategoryRecord;
      return null;
  },

  _getExceptionGlideRecord: function(tableName, sourceRecordId) {
      var exceptionRecord = new GlideRecord('sn_grc_exception');
      exceptionRecord.addQuery('table', tableName);
      exceptionRecord.addQuery('source_record', sourceRecordId);
      return exceptionRecord;
  },

  type: 'GRCExceptionUtilsBase'
};

Sys ID

d26d08090f805010bad14bb768767e3e

Offical Documentation

Official Docs: