Name

sn_mab_api.TemplateConfigRetrievalService

Description

The script include used for template mobile configuration retrievals

Script

var TemplateConfigRetrievalService = Class.create();
TemplateConfigRetrievalService.prototype = {
  initialize: function() {
      this.configResponse = new ConfigResponse();
      this.validationHandler = new ValidationHandler();
  },

  /*
  * The reason this "retrieveConfigTemplate" function exists is to avoid using the 
  * "Mobile Builder Configurations" records when retrieving individual, empty table schemas.
  */
  retrieveConfigTemplate: function(tableName, overrideValues) {
      if (!tableName)
          new ErrorHandler().throwBadRequestError('Invalid input received for getConfiguration: tableName is invalid.');
      
      // this manditory because we will always require an override for the sys_id
      if (!overrideValues)
          new ErrorHandler().throwBadRequestError('Invalid input received for getConfiguration: overrideValues is invalid.');

      if (!overrideValues.hasOwnProperty('sys_id'))
          new ErrorHandler().throwBadRequestError('Invalid input received for getConfiguration: overrideValues.sys_id is invalid.');
      
      var templateSysId = overrideValues.sys_id;
      var includedMetadataFields = overrideValues.includedMetadataFields ? overrideValues.includedMetadataFields : [];

      var templateConfigNode = this.getTemplateConfigNodeForTable(tableName, includedMetadataFields);
      if (overrideValues.hasOwnProperty('templateValues'))
          templateConfigNode = this.overrideTemplateValues(overrideValues.templateValues, templateConfigNode);

      this.configResponse.addConfigNode(tableName, templateSysId, templateConfigNode);

      return this.configResponse;
  },

  getTemplateConfigNodeForTable: function(tableName, includedMetadataFields) {
      if (!this.validationHandler.isValidTable(tableName))
          new ErrorHandler().throwNotFoundError('Does not exist for table: ' + tableName);
      
      if (!this.validationHandler.isTableAccessible(tableName))
          new ErrorHandler().throwBadRequestError('Is not accessible for table: ' + tableName);
      
      // we use the grs because this is a non-transactional function
      // if it were transactional we'd use the daoCache
      var grs = new GlideRecordSecure(tableName);
      grs.newRecord();
      var elements = grs.getElements();
      var returnObject = {};

      // value and displayValue are included because the front end expects them to be in the object
      elements.forEach(function(element) {
          var elementName = element.getName();
          var value = element && grs.getValue(elementName) || '';
          var elementTable = element.getTableName();
          var type = element.getED().getInternalType();
          var displayValue = element.getDisplayValue() || '';

          // // we do not want to provide metadata fields unless specificly requested.
          if (elementTable === 'sys_metadata' && includedMetadataFields.indexOf(elementName) === -1)
              return;
          
          returnObject[elementName] = {
              value: value,
              type: type,
              displayValue: displayValue
          };
      });

      return returnObject;
  },
  
  overrideTemplateValues: function(overrideTemplateValues, templateConfigNode) {
      var newTemplateConfigNode = templateConfigNode;
      // First, we itterate over the field names of a glide record that should be replaced.
      for (fieldName in overrideTemplateValues) {
          // Second, we itterate on the properties of the record that should be replaced.
          for (propertyName in overrideTemplateValues[fieldName]) {
              var override = overrideTemplateValues[fieldName][propertyName];

  			// We create the field if it does not exist so the FE may provide custome fields when needed
  			if (!newTemplateConfigNode.hasOwnProperty(fieldName))
  				newTemplateConfigNode[fieldName] = {};
  			
  			newTemplateConfigNode[fieldName][propertyName] = override;
          }
      }
      return newTemplateConfigNode;
  },
};

Sys ID

f3b0b4b60f632010e70a4abec4767e8f

Offical Documentation

Official Docs: