Name

sn_sow_em.IncidentsOnCiForRF

Description

Implements extension point sn_rf.ScriptingRuleInterface Use this interface to implement a script include for rule using Scripting evaluation type

Script

var IncidentsOnCiForRF = Class.create();
IncidentsOnCiForRF.prototype = Object.extendsObject(sn_rf.RFScriptingRuleSNC, {
  initialize: function() {
  	this.emRfCommons = new sn_sow_em.EmRfCommons();
  },
  
  /**********
   * Generates output schema for 'Scripting' evaluation type and returns the expected output structure of getEvaluationOutputs()
   * 
   * @return JSON containing list of output's metadata
   **********/
  getEvaluationOutputSchema: function() {
      return {
          'status': 'success',
          'schema': [
              // List of records to show in pill picker
              {
                  'name': this.emRfCommons.INCIDENT_RECORD_NAME,
                  'label': this.emRfCommons.INCIDENT_RECORD_LABEL,
                  'type': 'reference', // this value should be either 'string' or 'reference'
                  'referenceTable': 'incident'
              }
          ],
          'metaData': {
              'uniqueKey': [this.emRfCommons.INCIDENT_RECORD_NAME], // name of the fields to uniquely identify a recommendation
              'confidence': '' // name of the field which will have confidence score for the recommendation
          }
      };

  },

  /**********
   * Generates evaluation outputs for given record and inputs
   * 
   * @param param.contextRecord Current record for which we are evaluating
   * @return JSON containing list of evaluation outputs
   **********/
  getEvaluationOutputs: function(param) {
      var currAlertGr = param.contextRecord;
      
  	this.emRfCommons.logStartTime(this.type, currAlertGr.number);
      var startTimeGdt = new GlideDateTime(); 
  	
  	try {
          var incidentGr = new GlideRecord("incident");

          var emAiInsight = new global.EmAiInsight();
          emAiInsight.getSameCiTaskQuery(incidentGr, currAlertGr);// add query conditions for finding same CI tasks to incidentGr

          incidentGr.orderByDesc('sys_created_on');
          incidentGr.setLimit(this.emRfCommons.TOP_N_RESULTS); //default is 10 results
          incidentGr.query();

          // log number of results
  		this.emRfCommons.logNumberOfResults(this.type, currAlertGr.number, incidentGr.getRowCount());

          var evaluationOutputs = this.emRfCommons.insertIncidentsToResultArray(incidentGr, "sys_id");
  		
  		this.emRfCommons.logEndTime(this.type, currAlertGr.number, startTimeGdt);
          return {
              'status': 'success',
              'evaluationOutputs': evaluationOutputs
          };

      } catch (e) {
           var errorMessage = this.emRfCommons.logErrorMessage(this.type, currAlertGr.number, currAlertGr.getUniqueValue(), e);

          return {
              'status': 'error',
              'errorCode': 40001,
              'errorMessage': errorMessage
          };
      }
  },

  type: 'IncidentsOnCiForRF'
});

Sys ID

069b380c07410110b34ce06b0fd300b7

Offical Documentation

Official Docs: