Name

sn_install_base.InstallBaseProductModelDao

Description

A DAO class to get list of all the product models associated with the sold products and the install base where the context is a Consumer, Household, Account, Contact or Service Organization.

Script

var InstallBaseProductModelDao = Class.create();
InstallBaseProductModelDao.prototype = {


  initialize: function() {
      this.isSimpleOutput = true;

      /* Install base attributes */
      this.ibAttributeObj = {
          "install_base_id": "sys_id",
          "install_base_name": "name",
          "parent_install_base_id": "parent",
          "root_install_base_id": "root"
      };

      /* Sold product attributes */
      this.spAttributeObj = {
          "sold_product_id": "sys_id",
          "sold_product_name": "name",
          "parent_sold_product_id": "parent_sold_product"
      };

      /* Product attributes */
      this.productAttributeObj = {
          "product_model_id": "sys_id",
          "product_model_name": "display_name"
      };

      /*list of all sys_ids of sold products that are associated with install base items as installed products */
      this.soldProductsLinkedtoIBs = {};

      //filter out invalid allowedTables
      this.allowedTables = [
          global.CSMBaseConstants.ACCOUNT_TABLE,
          global.CSMBaseConstants.CONTACT_TABLE,
          global.CSMBaseConstants.CONSUMER_TABLE,
          global.CSMBaseConstants.HOUSEHOLD,
          global.CSMBaseConstants.SERVICE_ORGANIZATION_TABLE
      ];

  },

  /*
   * This method returns the list of all Product Models associated with the sold product and the install base item
   * @ Parameter - json input format
   * [
   *      {“table”:”customer_account”,”value”:”account_1_sysid”},
   *      {“table”:”customer_contact”,”value”:”contact_1_sysid”}
   * ]
   * Possible table values:
   * “customer_account”
   * “customer_contact”
   * “csm_consumer”
   * “cmn_household”
   * “sn_customer_service_organization”
   *
   * @searchTearm - partial search name for the product model
   * @count - max limit of the records to be returned.
   * Response format:
   * 
   *   @isSimpleOutput is true:- Simple Response
   * [
   *   {
   *    "product_model_id": "product_model_id1",
   *    "product_model_name": "product_model_name1" //Display Name
   *   }
   * ]
   * 
   *   @isSimpleOutput is false:- Complex Response
   * [
   *   {
   *    "install_base_id".      :"install_base_id1",
   *    "install_base_name"     :"install_base_name1",
   *    "parent_install_base_id":"parent_install_base_id1",
   *    "sold_product_id"       :"sold_product_id1",
   *    "sold_product_name"     :"sold_product_name1",
   *    "parent_sold_product_id":"parent_sold_product_id1",
   *    "root_install_base_id"  :"root_install_base_id1",
   *    "product_model_id"      :"product_model_id1",
   *    "product_model_name"    :"product_model_name1"
   *	 }
   * ]
   * 
   * Note: Complex Response holds the installed product details of an install base item or a sold product also.
   * 
   */
  getProductModels: function(context, searchTerm, count, isSimpleResponse) {
      if (gs.nil(count))
          count = global.CSMBaseConstants.DEFAULT_PROD_MODEL_API_REC_COUNT;

      this.soldProductsLinkedtoIBs = {};
      if (isSimpleResponse == false) {
          this.isSimpleOutput = false;
      } else {
          this.isSimpleOutput = true;
      }

      var inputVars = {};

      for (i = 0; i < context.length; i++) {
          if (this.allowedTables.indexOf(context[i].table) >= 0) {
              inputVars[context[i].table] = context[i].value;
          }
      }

      var products = [];

      if (Object.keys(inputVars).length == 0 || count == 0) {
          return products;
      }
      var productMap = new Object();

      /* Install base items */
      var installBaseGR = this._getGlideRecord(global.CSMBaseConstants.INSTALL_BASE_ITEM, inputVars, searchTerm, count);
      this._buildResponse(installBaseGR, productMap, products, count);

      var remainingCount;
      if (!gs.nil(count)) {
          if (this.isSimpleOutput)
              remainingCount = count - products.length;
          else
              remainingCount = count - installBaseGR.getRowCount();
      }

      /* Sold Products */
      if (gs.nil(count) || remainingCount > 0) {
          var soldProductGR = this._getGlideRecord(global.CSMBaseConstants.SOLD_PRODUCT, inputVars, searchTerm, remainingCount);
          this._buildResponse(soldProductGR, productMap, products, remainingCount);
      }

      return products;

  },

  _buildResponse: function(gr, prodMap, products, count) {
      if (this.isSimpleOutput) {
          this._buildSimpleResponse(gr, prodMap, products, count);
      } else {
          this._buildComplexResponse(gr, products, count);
      }
  },

  _buildSimpleResponse: function(gr, prodMap, products, count) {
      if (!gs.nil(gr)) {
          var pendingCount = count;
          var productSysID;
          while (gr.next() && (gs.nil(pendingCount) || pendingCount > 0)) {
              if (gr.canRead()) {
                  productSysID = gr.product.sys_id + "";
                  if (prodMap[productSysID]) continue;
                  productModel = {};
                  var productGr = new GlideRecordSecure(global.CSMBaseConstants.PRODUCT);
                  productGr.get(productSysID);
                  this.getFieldValues(productGr, productModel);
                  products.push(productModel);
                  prodMap[productModel.product_model_id] = productModel.product_model_name;
                  if (!gs.nil(pendingCount)) {
                      pendingCount--;
                  }
              }
          }
      }
  },

  _buildComplexResponse: function(gr, products, count) {
      if (!gs.nil(gr)) {
          var pendingCount = count;
          while (gr.next()) {
              if (gr.canRead() && (gs.nil(pendingCount) || pendingCount > 0)) {
                  var productGr = new GlideRecordSecure(global.CSMBaseConstants.PRODUCT);
                  productGr.get(gr.product.sys_id + "");
                  var productObj = {};
                  this.getFieldValues(productGr, productObj); //product info

                  this.getFieldValues(gr, productObj); //IB or SP info

                  if (gr.getTableName() == global.CSMBaseConstants.INSTALL_BASE_ITEM) {
                      this.getFieldValues(null, productObj, global.CSMBaseConstants.SOLD_PRODUCT);
                      var soldProductsLinkedtoCurrentIB = [];
                      if (!this.getSPIdsFromInstalledProducts(gr, soldProductsLinkedtoCurrentIB, pendingCount)) {
                          products.push(productObj);
                          continue;
                      }
                      var spGr = new GlideRecordSecure(global.CSMBaseConstants.SOLD_PRODUCT);
                      spGr.addQuery('sys_id', 'IN', soldProductsLinkedtoCurrentIB.join(','));
                      spGr.query();
                      if (!gs.nil(pendingCount)) {
                          pendingCount = pendingCount - spGr.getRowCount();
                      }
                      while (spGr.next()) {
                          var productWithSP = JSON.parse(JSON.stringify(productObj));
                          this.getFieldValues(spGr, productWithSP); //sold product info
                          products.push(productWithSP);
                          this.soldProductsLinkedtoIBs[spGr.sys_id] = true;
                      }
                  } else if (gr.getTableName() == global.CSMBaseConstants.SOLD_PRODUCT) {
                      this.getFieldValues(null, productObj, global.CSMBaseConstants.INSTALL_BASE_ITEM);
                      this.soldProductsLinkedtoIBs[gr.sys_id] = true;
                      products.push(productObj);
                      if (!gs.nil(pendingCount)) {
                          pendingCount--;
                      }
                  }
              }
          }
      }
  },

  getSPIdsFromInstalledProducts: function(gr, soldProductsLinkedtoCurrentIB, count) {
      var installedProducts = new GlideRecordSecure(global.CSMBaseConstants.M2M_INSTALLED_PRODUCT);
      installedProducts.addQuery('install_base_item', gr.sys_id + "");
      if (!gs.nil(count)) {
          installedProducts.setLimit(count);
      }
      installedProducts.query();
      var doIPsExist = installedProducts.hasNext();
      while (installedProducts.next()) {
          soldProductsLinkedtoCurrentIB.push(installedProducts.sold_product.sys_id);
      }
      return doIPsExist;
  },

  getFieldValues: function(gr, productListObject, table) {
      table = table || gr.getTableName();
      var attributes = {};
      switch (table) {
          case global.CSMBaseConstants.INSTALL_BASE_ITEM:
              attributes = this.ibAttributeObj;
              break;
          case global.CSMBaseConstants.SOLD_PRODUCT:
              attributes = this.spAttributeObj;
              break;
          case global.CSMBaseConstants.PRODUCT:
              attributes = this.productAttributeObj;
              break;
      }
      for (var attributeName in attributes) {
          productListObject[attributeName] = !gs.nil(gr) ? gr.getElement(attributes[attributeName]).toString() : "";
      }
  },

  _getGlideRecord: function(table, inputVars, searchTerm, count) {
      var encodedQuery = 'product!=NULL';

      if (!gs.nil(searchTerm))
          encodedQuery += '^product.display_name' + 'CONTAINS' + searchTerm;

      if (table == global.CSMBaseConstants.INSTALL_BASE_ITEM) {
          var ibEncodedQuery = new sn_install_base.InstallBaseFilter().getRefQualifierForIBonCase(inputVars);
          if (ibEncodedQuery != '')
              encodedQuery += '^' + ibEncodedQuery;
      } else if (table == global.CSMBaseConstants.SOLD_PRODUCT) {
          if (this.soldProductsLinkedtoIBs != {})
              encodedQuery += '^sys_id' + 'NOT IN' + Object.keys(this.soldProductsLinkedtoIBs).join(',');

          var case_record = new GlideRecord(global.CSMBaseConstants.CASE_TABLE);
          case_record.initialize();
          var fields = ['account', 'contact', 'consumer', 'household', 'requesting_service_organization'];
          for (i = 0; i < fields.length; i++) {
              if (!gs.nil(inputVars[this.allowedTables[i]])) {
                  case_record[fields[i]] = inputVars[this.allowedTables[i]];
              }
          }
          var spEncodedQuery = new sn_install_base.SoldProductFilter().getSoldProductRefQualifier(case_record);
          if (spEncodedQuery != '')
              encodedQuery += '^' + spEncodedQuery;
          if (inputVars[global.CSMBaseConstants.HOUSEHOLD]) {
              encodedQuery += '^' + 'household=' + inputVars[global.CSMBaseConstants.HOUSEHOLD];
          }
      }

      var gr = new GlideRecord(table);
      gr.addQuery(encodedQuery);

      if (!this.isSimpleOutput && !gs.nil(count))
          gr.setLimit(count);

      gr.query();
      return gr;
  },

  type: 'InstallBaseProductModelDao'
};

Sys ID

58b9388bd4d11910f8777df2f2ae05fb

Offical Documentation

Official Docs: