Name

sn_rf.RFExperienceService

Description

Class for sn_rf_recommendation_experience table utility functions.

Script

var RFExperienceService = Class.create();
RFExperienceService.getByRule = function(rule, experienceTypes) {
  var experiences = [];
  var gr = new GlideRecord(RFConstants.tables.SN_RF_RECOMMENDATION_EXPERIENCE);
  gr.addActiveQuery();
  gr.addQuery(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_SHOW_AS, RFConstants.queryConstants.IN, experienceTypes);
  gr.addQuery(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_RULE, rule.getSysID());
  gr.orderBy(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_ORDER);
  gr.query();
  while (gr.next()) {
      experiences.push(new RFExperienceService(gr.getValue(RFConstants.fields.SYS_ID)));
  }
  return experiences;
};

RFExperienceService.prototype = {
  tableName: RFConstants.tables.SN_RF_RECOMMENDATION_EXPERIENCE,

  initialize: function(grOrSysId) {
      this.rfLogger = new RFLogger("RFExperienceService");
      if (grOrSysId && grOrSysId.sys_class_name == this.tableName) {
          this.currentRecord = grOrSysId;
      } else if (grOrSysId) {
          var expGr = new GlideRecord(this.tableName);
          if (expGr.get(grOrSysId)) {
              this.currentRecord = expGr;
          }
      }
  },

  hasValidRecord: function() {
      return this.currentRecord && this.currentRecord.sys_class_name == this.tableName;
  },

  isActive: function() {
      return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_ACTIVE);
  },

  getSysId: function() {
      return this.currentRecord.getValue(RFConstants.fields.SYS_ID);
  },

  getShowAs: function() {
      return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_SHOW_AS);
  },

  isMessageCard: function() {
      return this.getShowAs() == RFConstants.experienceType.MESSAGE_CARD;
  },

  isRecordCard: function() {
      return this.getShowAs() == RFConstants.experienceType.RECORD_CARD;
  },

  isFieldLevel: function() {
      return this.getShowAs() == RFConstants.experienceType.FIELD_LEVEL;
  },

  getRuleSysId: function() {
      return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_RULE);
  },

  getRule: function() {
      var ruleId = this.getRuleSysId();
      if (ruleId) {
          var rule = new RFRuleService(ruleId);
          if (rule.hasValidRecord()) {
              return rule;
          }
      }
  },

  getLabel: function() {
      if (this.isMessageCard()) {
          return this.currentRecord.getDisplayValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_LABEL);
      }
  },

  getTitle: function() {
      if (this.isMessageCard()) {
          return this.currentRecord.getDisplayValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_TITLE);
      }
  },

  getMessage: function() {
      if (this.isMessageCard()) {
          return this.currentRecord.getDisplayValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_MESSAGE);
      }
  },

  getShowRecord: function() {
      if (this.isRecordCard()) {
          return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_SHOW_RECORD);
      }
  },

  getRecordDisplayConfigurationSysId: function() {
      if (this.isRecordCard()) {
          return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_RECORD_DISPLAY_CONFIGURATION);
      }
  },

  getTable: function() {
      if (this.isFieldLevel()) {
          return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_TABLE);
      }
  },

  getOnField: function() {
      if (this.isFieldLevel()) {
          return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_ON_FIELD);
      }
  },

  getFieldMessage: function() {
      if (this.isFieldLevel()) {
          return this.currentRecord.getDisplayValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_FIELD_MESSAGE);
      }
  },

  getThresholdForStamping: function() {
      if (this.isFieldLevel()) {
          var fieldName = RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_THRESHOLD;
          var configurationPropertyName = RFUtils.getConfigurationPropertyName(this.tableName, fieldName, this.getSysId());
          var configurationPropertyValue = gs.getProperty(configurationPropertyName);
          if (configurationPropertyValue == null) {
              var thresholdValue = this.currentRecord.getValue(fieldName);
          } else {
              configurationPropertyValue = configurationPropertyValue.trim();
              if (!isNaN(configurationPropertyValue)) {
                  thresholdValue = configurationPropertyValue;
              }
          }
          return thresholdValue ? Number(thresholdValue) : null;
      }
  },

  getStampValue: function() {
      if (this.isStamping()) {
          return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_STAMP_VALUE);
      }
  },

  isStamping: function() {
      var thresholdValue = this.getThresholdForStamping();
      return thresholdValue === 0 || Boolean(thresholdValue);
  },

  canHaveAction: function() {
      return this.isRecordCard() || this.isMessageCard();
  },

  getPrimaryActionId: function() {
      return this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_PRIMARY_ACTION);
  },

  getPrimaryAction: function() {
      var primaryActionId = this.getPrimaryActionId();
      if (this.canHaveAction() && primaryActionId) {
          var action = new RFActionService(primaryActionId);
          if (action.hasValidRecord()) {
              return action;
          }
      }
  },

  getPrimaryActionDetails: function(contextRecord, evaluationOutputs) {
      var details = [];
      var primaryAction = this.getPrimaryAction();
      if (primaryAction) {
          var detail = primaryAction.getDetails(contextRecord, evaluationOutputs, this.getSysId(), this.fromHistory);
          if (detail) {
              details.push(detail);
          }
      }
      return details;
  },

  getOtherActions: function() {
      var actions = [];
      var otherActions = this.currentRecord.getValue(RFConstants.fields.SN_RF_RECOMMENDATION_EXPERIENCE_OTHER_ACTIONS);
      if (this.canHaveAction() && otherActions) {
          var actionsIds = otherActions.split(",");
          for (var i = 0; i < actionsIds.length; i++) {
              var action = new RFActionService(actionsIds[i]);
              if (action.hasValidRecord()) {
                  actions.push(action);
              }
          }
      }
      return actions;
  },

  getOtherActionsDetails: function(contextRecord, evaluationOutputs) {
      var actionDetails = [];
      var actions = this.getOtherActions();
      for (var i = 0; i < actions.length; i++) {
          var isCompleted = RFHistoryService.isCompleted(contextRecord, this.getSysId(), evaluationOutputs, actions[i].getSysId());
          var details = actions[i].getDetails(contextRecord, evaluationOutputs, this.getSysId(), isCompleted);
          if (details) {
              actionDetails.push(details);
          }
      }
      if (!this.fromHistory) {
          actionDetails.push(this._getDiscardActionDetails(evaluationOutputs));
      }
      return actionDetails;
  },

  _getDiscardActionDetails: function(evaluationOutputs) {
      var details = {};
      details[RFConstants.actionDetails.TYPE] = RFConstants.actionType.DISCARD;
      details[RFConstants.actionDetails.ACTION_SYS_ID] = RFConstants.actionType.DISCARD;
      details[RFConstants.actionDetails.LABEL] = RFConstants.DISCARD_LABEL;
      details[RFConstants.actionDetails.IS_UNDO] = false;
      details[RFConstants.actionDetails.RULE_OUTPUTS] = RFActionService.serializeEvaluationOutput(evaluationOutputs);
      details[RFConstants.actionDetails.ADDITIONALPARAMETERS] = {};
      details[RFConstants.actionDetails.ADDITIONALPARAMETERS][RFConstants.actionDetails.EXPERIENCE_ID] = this.getSysId();
      details[RFConstants.actionDetails.ADDITIONALPARAMETERS] = JSON.stringify(details[RFConstants.actionDetails.ADDITIONALPARAMETERS]);
      return details;
  },

  getDetails: function(contextRecord, evaluationOutputs, fromHistory, options) {
      if (this.hasValidRecord()) {
          this.fromHistory = fromHistory;
          try {
              var isCompleted = !fromHistory && RFHistoryService.isCompleted(contextRecord, this.getSysId(), evaluationOutputs);
              if (isCompleted) {
                  this.rfLogger.logDebug("Recommendation experience " + this.getSysId() + " for context record " + contextRecord.sys_id + " is moved to history");
                  return;
              }
              options = options || {};
              var expDetails = {};
              expDetails[RFConstants.experienceDetails.SHOW_AS] = this.getShowAs();
              if (options.discarded) {
                  expDetails[RFConstants.experienceDetails.TAG] = RFConstants.historyStatus.DISCARDED;
              }

              if (this.isMessageCard()) {
                  expDetails["label"] = this._parseValue(this.getLabel(), contextRecord, evaluationOutputs);
                  expDetails["title"] = this._parseValue(this.getTitle(), contextRecord, evaluationOutputs);
                  expDetails["message"] = this._parseValue(this.getMessage(), contextRecord, evaluationOutputs);
              } else if (this.isRecordCard()) {
                  var showRecord = this._parseValue(this.getShowRecord(), contextRecord, evaluationOutputs, true);
                  if (!showRecord || !showRecord.isValidRecord) {
                      this.rfLogger.logDebug("Invalid show record value for experience " + this.getSysId());
                      return;
                  }

                  var rdConfGR = new RFRecordDisplayConfigurationService(this.getRecordDisplayConfigurationSysId(), showRecord);
                  if (!rdConfGR.hasValidRecord()) {
                      this.rfLogger.logDebug("Invalid record display configuration " + this.getRecordDisplayConfigurationSysId() +
                          " for show record " + showRecord.sys_id + " of table " + showRecord.getTableName() + " in experience " + this.getSysId());
                      return;
                  }

                  expDetails["label"] = rdConfGR.getCardLabel();
                  expDetails["title"] = rdConfGR.getCardTitle();
                  expDetails["description"] = rdConfGR.getCardDescription();
                  expDetails["recordTable"] = showRecord.getTableName();
                  expDetails["recordSysId"] = showRecord.getValue(RFConstants.fields.SYS_ID);
                  expDetails["fields"] = rdConfGR.getCardAdditionalFields();
                  expDetails["footer"] = rdConfGR.getCardDisplayFooter();

                  var isDefaultDetailView = rdConfGR.isDefaultDetailView();
                  if (isDefaultDetailView) {
                      expDetails["detail"] = {
                          title: rdConfGR.getDetailTitle(),
                          link: rdConfGR.getDetailLink(),
                          fields: rdConfGR.getDetailAdditionalFields(),
                          lastWorkNote: rdConfGR.getDetailDisplayWorkNote()
                      };
                  }
              } else if (this.isFieldLevel()) {
                  expDetails[RFConstants.experienceDetails.ON_FIELD] = this.getOnField();
                  expDetails[RFConstants.experienceDetails.MESSAGE] = this._parseValue(this.getFieldMessage(), contextRecord, evaluationOutputs);
                  if (this.isStamping()) {
                      var stampDetails = {};
                      stampDetails[RFConstants.experienceDetails.IS_CONFIDENT] = false;
                      var threshold = this.getThresholdForStamping();
                      var confidence = evaluationOutputs[RFConstants.evaluatorVariable.CONFIDENCE_INTERNAL];
                      if (threshold <= Number(confidence)) {
                          var stampValue = this._parseValue(this.getStampValue(), contextRecord, evaluationOutputs, true);
                          if (stampValue.isValidRecord) {
                              stampDetails[RFConstants.experienceDetails.VALUE] = stampValue.getValue(RFConstants.fields.SYS_ID);
                              stampDetails[RFConstants.experienceDetails.DISPLAY_VALUE] = stampValue.getDisplayValue();
                          } else {
                              stampDetails[RFConstants.experienceDetails.VALUE] = stampValue;
                          }
                          stampDetails[RFConstants.experienceDetails.IS_CONFIDENT] = true;
                      }
                      expDetails[RFConstants.experienceDetails.STAMP_DETAILS] = stampDetails;
                  }
              }

              if (this.canHaveAction()) {
                  expDetails["primaryActions"] = options.discarded ? [] : this.getPrimaryActionDetails(contextRecord, evaluationOutputs);
                  expDetails["otherActions"] = options.discarded ? [] : this.getOtherActionsDetails(contextRecord, evaluationOutputs);
              }
              return expDetails;
          } catch (e) {
              this.rfLogger.logError('Error occurred while executing getDetails for Experience ' + this.getSysId() + '. Error: ' + e);
          }
      }
  },

  _parseValue: function(unParsedString, contextRecord, evaluationOutputs, getGlideRecord) {
      var parsedString = "";
      if (unParsedString) {
          parsedString = new RFPillParserUtils().getParsedValue(unParsedString, contextRecord, evaluationOutputs, getGlideRecord);
      }
      return parsedString;
  },

  type: "RFExperienceService"
};

Sys ID

e30c0d7ac7033010dd7ab6c427c26099

Offical Documentation

Official Docs: