Name

global.CIRecordPagePropsUtil

Description

No description available

Script

var CIRecordPagePropsUtil = Class.create();
CIRecordPagePropsUtil.prototype = {
  initialize: function() {
      this.ciPagePropsMetadata = new CIRecordPagePropsMetadata();
  },
  _isNewRecord: function(sysId) {
      if (sysId.startsWith('-')) return true;
      return false;
  },
  _isTaggingSupported: function(table, sysId) {
      try {
          if (this._isNewRecord(sysId)) return false;
          if (this.ciPagePropsMetadata.BLACKLIST_TAGS_FROM_SUPPORTED_TABLES.indexOf(table) >= 0) return false;
          return true;
      } catch (e) {
          return false;
      }
  },
  _isFormSupported: function(table) {
      try {
          //check for static list
          var supportedList = this.ciPagePropsMetadata.SUPPORTED_TABLES;
          if (supportedList.indexOf(table) >= 0) return true;

          //check for view based support
          var viewName = this.ciPagePropsMetadata.SUPPORTED_TABLES_VIEW;
          var formViewGr = new GlideRecord('sys_ui_section');
          formViewGr.addEncodedQuery('view.name=' + viewName + '^name=' + table);
          formViewGr.query();
          if (formViewGr.next()) return true;

          //check for sys_property
          var supportedListViaPropValue = this.ciPagePropsMetadata.SUPPORTED_TABLES_VIA_PROP;
          if (supportedListViaPropValue) {
              var supportedListViaProp = supportedListViaPropValue.split(',').map(function(table) {
                  return table.trim();
              });
              if (supportedListViaProp.indexOf(table) >= 0) return true;
          }
          return false;
      } catch (e) {
          return false;
      }
  },
  _getScopeProperties: function(table, sysId) {
      var scopeProps = {
          userScope: '',
          tableScope: '',
          tableScopeName: '',
          scopeMatched: false
      };
      if (!sysId) return scopeProps;

      var _scope = gs.getCurrentApplicationId();
      scopeProps.userScope = _scope == 'rhino.global' ? 'global' : _scope;
      scopeProps.tableScope = _scope;
      scopeProps.tableScopeName = _scope == 'global' ? 'Global' : '';
      var dbObjectGr = new GlideRecord(table);
      if (dbObjectGr.get(sysId)) {
          scopeProps.tableScope = dbObjectGr.sys_scope.toString();
          if (!scopeProps.tableScope) {
              scopeProps.tableScope = 'global';
              scopeProps.tableScopeName = 'Global';
          } else {
              scopeProps.tableScopeName = dbObjectGr.getDisplayValue('sys_scope');
          }
      }
      scopeProps.scopeMatched = _scope == scopeProps.tableScope;
      return scopeProps;
  },
  _getTableName: function(table) {
      var metadata = this.ciPagePropsMetadata.TABLE_METADATA;
      if (metadata[table]) return metadata[table].heading;
      var sysDbGr = new GlideRecord('sys_db_object');
      sysDbGr.addQuery('name', table);
      sysDbGr.query();
      if (sysDbGr.next()) {
          return sysDbGr.getValue('label');
      }
      return table;
  },
  _getRecordDisplayValue: function(table, sysId) {
      var tableGr = new GlideRecord(table);
      if (tableGr.get(sysId)) {
          return tableGr.getDisplayValue();
      }
      return sysId;
  },
  _getEntryPage: function(route) {
      var entries = this.ciPagePropsMetadata.CRUMBS;
      var defaultLandingRoute = this.ciPagePropsMetadata.DEFAULT_LANDING_ROUTE;
      if (route && entries[route]) return entries[route];
      return entries[defaultLandingRoute];
  },
  _getProps: function(table) {
      var response = {
          'entry': '',
          'supported': false,
          'heading': '',
          'title': '',
          'subheading': '',
          'filter': '',
          'view': ''
      };
      var _self = this;
      //check whether the table is mapped
      if (this.ciPagePropsMetadata.TABLE_METADATA[table]) {
          var _tableMetaData = _self.ciPagePropsMetadata.TABLE_METADATA[table];
          response.supported = true;
          response.entry = _self._getEntryPage(_tableMetaData.entry);
          response.heading = _tableMetaData.heading;
          response.title = _tableMetaData.title;
          response.subheading = _tableMetaData.subHeading;
          response.list_filter = _tableMetaData.list_filter;
          response.list_view = _tableMetaData.list_view;
          response.form_filter = _tableMetaData.form_filter;
          response.from_view = _tableMetaData.from_view;
          response.related_list_table = _tableMetaData.related_list_table;
          response.custom_form = _tableMetaData.custom_form;
          response.custom_list = _tableMetaData.custom_list;
      } else {
          response.supported = false;
          response.entry = _self._getEntryPage();
          response.heading = _self._getTableName(table);
          response.title = response.heading;
          response.subheading = '';
          response.list_filter = '';
          response.list_view = '';
          response.form_filter = '';
          response.from_view = '';
          response.related_list_table = '';
          response.custom_form = '';
          response.custom_list = '';
      }
      return response;
  },
  _getRelatedListParentTableRecordSysId: function(table, sysId, query, field) {
      try {
          if (this._isNewRecord(sysId) && query) {
              query = decodeURIComponent(query);
              var regex = new RegExp(field + '=(.*?)(?:\\^|\\^OR|$)', 'g');
              var matches = regex.exec(query);
              if (matches && matches[1]) return matches[1];
              return '';
          }
          var gr = new GlideRecord(table);
          if (gr.get(sysId)) {
              return gr.getValue(field);
          }
          return '';
      } catch (e) {
          return '';
      }
  },
  _getRelatedListBreadcrumbPath: function(type, table, sysId) {
      var _self = this;
      switch (type) {
          case 'list':
              return _self.ciPagePropsMetadata.CUSTOM_TAG + '#' + _self.ciPagePropsMetadata.DEFAULT_LIST + '#' + table;
          case 'form':
              var _formRoute = _self.ciPagePropsMetadata.TABLE_METADATA[table] && _self.ciPagePropsMetadata.TABLE_METADATA[table].custom_form;
              return _self.ciPagePropsMetadata.CUSTOM_TAG + '#' + (_formRoute || _self.ciPagePropsMetadata.DEFAULT_FORM) + '#' + (_formRoute ? '' : table) + '#' + sysId;
          default:
              return '';
      }
  },
  _generateBreadcrumbs: function(props, table, sysId, query, rlt) {
      var breadcrumbs = [], _self = this;
      var title = props.title;
      if (props.related_list_table) {
          var splittedRlt = props.related_list_table.split('.');
          var rltField = splittedRlt[0];
          var rltTable = splittedRlt[1];
          var rltRecord = _self._getRelatedListParentTableRecordSysId(table, sysId, query, rltField);
          breadcrumbs = _self._generateBreadcrumbs(_self._getProps(rltTable), rltTable, rltRecord, null, true).breadcrumbs;
      } else {
          breadcrumbs.push(props.entry);
          // Set the list view page of the table only for supported fields
          if (props.supported) {
              breadcrumbs.push({
                  page: rlt ? _self._getRelatedListBreadcrumbPath('list', table, sysId) : (props.custom_list || _self.ciPagePropsMetadata.DEFAULT_LIST),
                  label: title,
                  filter: props.list_filter,
                  view: props.list_view
              });
          }
      }

      // Set the table's record form page (iff sysId exists)
      if (sysId) {
          title = (this._isNewRecord(sysId)) ? _self.ciPagePropsMetadata.DEFAULT_NEW_RECORD_LABEL : (_self._getRecordDisplayValue(table, sysId) || _self.ciPagePropsMetadata.DEFAULT_EMPTY_RECORD_LABEL);
          breadcrumbs.push({
              page: rlt ? _self._getRelatedListBreadcrumbPath('form', table, sysId) : _self.ciPagePropsMetadata.DEFAULT_FORM,
              label: title,
              filter: props.form_filter,
              view: props.form_view
          });
      }
      return {
          'breadcrumbs': breadcrumbs,
          'title': title
      };
  },
  _hasWritePermission: function(table, sysId) {
      var gr = new GlideRecord(table);
      if (gr.get(sysId)) {
          return gr.canWrite();
      }
      return false;
  },
  _getPageProps: function(table, sysId, query) {
      var _self = this,
          pageProps = {},
          title;

      // Entry point
      var _props = _self._getProps(table);
      var breadcrumbs = _self._generateBreadcrumbs(_props, table, sysId, query, false);
      var userScope = _self._getScopeProperties(table, sysId).userScope;
      var userScopeName = _self._getScopeName(userScope);

      pageProps = {
          title: breadcrumbs.title,
          heading: _props.heading,
          subHeading: _props.subheading,
          breadcrumbs: breadcrumbs.breadcrumbs,
          filter: _props.filter,
          view: _props.view,
          is_scope_not_matched: !_self._getScopeProperties(table, sysId).scopeMatched,
          user_scope: userScope,
          record_scope: _self._getScopeProperties(table, sysId).tableScope,
          user_scope_name: userScopeName,
          record_scope_name: _self._getScopeProperties(table, sysId).tableScopeName,
          canWrite: _self._hasWritePermission(table, sysId),
          is_form_supported: _self._isFormSupported(table)
      };
      pageProps.hide_tags = !(pageProps.is_form_supported && _self._isTaggingSupported(table, sysId));
      return pageProps;
  },

  _getScopeName: function(scopeId) {
      if (scopeId && scopeId == 'global') {
          return 'Global';
      }
      if (scopeId) {
          var app = new GlideRecord('sys_store_app');
          if (app.get(scopeId)) {
              return app.getValue('name');
          }
          return '';
      }
  },
  getPageProps: function(table, sysId, query) {
      try {
          return this._getPageProps(table, sysId, query);
      } catch (e) {
          return [];
      }
  },
  _fetchDefaultValueForScript: function(table, field) {
      var dictionaryGr = new GlideRecord('sys_dictionary');
      dictionaryGr.addQuery('name', table.toString());
      dictionaryGr.addQuery('element', field);
      dictionaryGr.query();
      if (dictionaryGr.next()) return dictionaryGr.getValue('default_value');
      return '';
  },
  _fetchValuesForField: function(gr, fieldMetaData, fieldType, isNewRecord) {
      var field = fieldMetaData.field_name,
          _self = this;
      switch (fieldType) {
          case 'script':
              return isNewRecord ? _self._fetchDefaultValueForScript(gr.getTableName(), fieldMetaData.field_name) : gr.getValue(field);
          case 'slushbucket':
              //Input: Comma separated <sys_id>,
              //Output: [{id: <sys_id>, label: <display_value>}, ...]
              if (isNewRecord) return [];
              var userData = gr.getValue(field),
                  userDataFormatted = [];
              if (!userData) return null;
              userData = userData.split(',');
              fieldMetaData.field_data_source.forEach(function(group) {
                  if (userData.indexOf(group.id) != -1) userDataFormatted.push(group);
              });
              return userDataFormatted;
      }
      return null;
  },
  /*
   * Get all configured fields which are not supported by UIB Record Page Tabs/Form component
   * params:
   * table {string}: Name of the table
   * recordSysId {string} SysId of the record
   * preFetchUserData {boolean} If true, the field is pre-populated with the field data. The data is available under 'field_data' key
   */
  getFields: function(table, recordSysId, preFetchUserData) {
      try {
          if (!table || !recordSysId) return null;
          var allFields = this.ciPagePropsMetadata.SUPPORTED_CUSTOM_FIELDS[table];
          if (!preFetchUserData) return allFields;
          var tableGr = new GlideRecord(table);
          if (!this._isNewRecord(recordSysId) && (!tableGr.get(recordSysId) || !tableGr.canRead())) return allFields;
          for (var fieldType in allFields) {
              allFields[fieldType].field_data = this._fetchValuesForField(tableGr, allFields[fieldType], fieldType, this._isNewRecord(recordSysId));
          }
          return allFields;
      } catch (e) {
          return {};
      }
  },
  type: 'CIRecordPagePropsUtil'
};

Sys ID

acc44b0bc30270104b8e88c7c840ddbc

Offical Documentation

Official Docs: