Name

global.AccountImpl

Description

All functionalities for the Consumer APIs consolidated in this implementation class

Script

var AccountImpl = Class.create();
AccountImpl.prototype = {
  initialize: function() {
      this.accountDao = new global.AccountDao();
  },

  getChildAccounts: function() {
      var account = gs.getUser().getCompanyID();
      var key = "child_accounts_" + account;
      var accounts = gs.getSession().getClientData(key);
      var cacheUtil = new global.AccountsCacheUtil();
      var canUsePrivateCache = cacheUtil.isLargeParentAccount(account) && this._isPrivateCachingEnabledForAccounts();
      if (gs.nil(accounts) && canUsePrivateCache) {
          cacheUtil.initPrivateCache(global.CSMBaseConstants.CACHE_CATALOG_CHILD_ACCOUNTS);
          accounts = GlideCacheManager.get(global.CSMBaseConstants.CACHE_CATALOG_CHILD_ACCOUNTS, key);
      }
      if (gs.nil(accounts)) {
          accounts = new global.CSQueryBRUtil().getSubAccounts();
          if (canUsePrivateCache)
              GlideCacheManager.put(global.CSMBaseConstants.CACHE_CATALOG_CHILD_ACCOUNTS, key, accounts);
          else
              gs.getSession().putClientData(key, accounts);
      }
      return accounts;
  },

  getAccountInfoBySearchString: function(searchQueryStr, offset, limit) {
      return this.accountDao.getAccountBySearchQueryString(searchQueryStr, offset, limit);
  },

  getAccountInfoById: function(number, displayValue) {
      return this.accountDao.getAccountById(number, displayValue);
  },

  getAccountsFromTeamMember: function() {
      var key = 'accounts_from_team_member_' + gs.getUserID();
      var accountsFromTeamMember = gs.getSession().getClientData(key);
      if (!gs.nil(accountsFromTeamMember))
          return (accountsFromTeamMember + "").split(",");

      var accounts = {};

      var accountUserReln = new GlideRecord("sn_customerservice_team_member");
      accountUserReln.addQuery("user", gs.getUserID());
      accountUserReln.addQuery("responsibility", "b2d87bedb3730010700b4d43c6a8dca8");
      accountUserReln.query();
      while (accountUserReln.next())
          accounts[accountUserReln.account.toString()] = true;

      var accountsArray = Object.keys(accounts);
      if (accountsArray != null && accountsArray.length > 0) {
          gs.getSession().putClientData(key, accountsArray.join(","));
      }

      return accountsArray;
  },


  getAccountsFromContactRelationship: function() {
      //account from contact relationship
      var key = 'accounts_from_contact_rel_' + gs.getUserID();

      var accountsFromContactRel = gs.getSession().getClientData(key);
      if (!gs.nil(accountsFromContactRel))
          return (accountsFromContactRel + "").split(",");


      var accounts = {};
      var ac = new GlideRecord('sn_customerservice_contact_relationship');
      ac.addQuery('contact', gs.getUserID());
      ac.query();
      while (ac.next())
          accounts[ac.getValue('company').toString()] = true;

      var accountsArray = Object.keys(accounts);
      if (accountsArray != null && accountsArray.length > 0) {
          gs.getSession().putClientData(key, accountsArray.join(","));
      }

      return accountsArray;
  },

  getAccountsFromAccountRelationship: function() {
      var useSessionCache = true;
      var key = 'accounts_from_account_rel_' + gs.getUserID();
      var accountsFromAccountRel = gs.getSession().getClientData(key);

      // Cache accounts from account relationship in a private cache instead of session cache 
      // if 'use_accounts_private_cache' property is enabled
      var cacheUtil = new global.AccountsCacheUtil();
      if (gs.nil(accountsFromAccountRel) && this._isPrivateCachingEnabledForAccounts()) {
          var cacheName = 'ACCOUNTS_FROM_ACCOUNT_REL';
          key = 'accounts_from_account_rel_' + gs.getUser().getCompanyID();
          cacheUtil.initPrivateCache(cacheName);

          // customer_account is the table on which Query condition is defined
          if (cacheUtil.isInPrivateCache(cacheName, key) || cacheUtil.useAccountsPrivateCache("customer_account")) {
              useSessionCache = false;
              accountsFromAccountRel = GlideCacheManager.get(cacheName, key);
          }
      }

      if (!gs.nil(accountsFromAccountRel))
          return (accountsFromAccountRel + "").split(",");

      //account from account relationship
      var accounts = {};
      var ar = new GlideRecord('account_relationship');
      ar.addQuery('reverse_relationship', false);
      ar.addQuery('from_company', gs.getUser().getCompanyID());
      ar.query();
      while (ar.next())
          accounts[ar.getValue("to_company").toString()] = true;

      var accountsArray = Object.keys(accounts);
      if (accountsArray != null && accountsArray.length > 0) {
          if (!useSessionCache)
              GlideCacheManager.put('ACCOUNTS_FROM_ACCOUNT_REL', key, accountsArray.join(","));
          else
              gs.getSession().putClientData(key, accountsArray.join(","));
      }

      return accountsArray;
  },
  
  getAccountPath: function() {
      account = gs.getUser().getCompanyID();
      if (gs.nil(account)) {
          return null;
      }

      var key = "accounts_path_" + account;
      var path = gs.getSession().getClientData(key);
      if (gs.nil(path)) {
          var gr = new GlideRecord("customer_account");
          if (gr.get(account))
              path = gr.getValue('account_path');
          gs.getSession().putClientData(key, gs.nil(path) ? 'NIL' : path);
      }
      return path == 'NIL' ? null : path;
  },

  getAccountsFromAccountConsumer: function() {
      var accounts = {};
      var consumerId = '';
      var consumer = new GlideRecord('csm_consumer');
      consumer.addQuery('user', gs.getUserID());
      consumer.query();
      if (consumer.next()) {
          consumerId = consumer.sys_id;
          var aco = new GlideRecord('sn_acct_consumer_account_consumer');
          aco.addQuery('consumer', consumerId);
          aco.addActiveQuery();
          aco.query();
          while (aco.next())
              accounts[aco.getValue("account").toString()] = true;
      }
      var accountsArray = Object.keys(accounts);
      return accountsArray;
  },

  getUserAccount: function() {
      return gs.getUser().getCompanyID();
  },

  /* 
   * If user is a customer then it returns my-account, customer relationships
   * If user is a partner then it returns my-account, customer relationships, and account relationships
   */
  getMyAccountsWithManagedAccess: function() {
      var isPartner = gs.hasRole("sn_customerservice.partner");
      return this.getMyAccountsWithAccess(true, true, true, isPartner);
  },

  /* 
   * If user is a customer then it returns my-account, customer relationships
   * If user is a partner then it returns my-account, customer relationships, and account relationships
   */
  getMyAccountsWithoutManagedAccess: function() {
      var isPartner = gs.hasRole("sn_customerservice.partner");
      return this.getMyAccountsWithAccess(false, true, true, isPartner);
  },

  //Either it will return accounts with managed access or without managed access based on the flag withManagedAccess, but not both.
  getMyAccountsWithAccess: function(withManagedAccess, countMe, getContactRelationships, getCustomerAccounts) {
      var accountsObj = this._getMyAccountsAsObject(countMe, getContactRelationships, getCustomerAccounts);
  	
      if (Object.keys(accountsObj).length == 0)
          return null;

      var managedAccessAccounts = this._getAccountsWithManageAccess(Object.keys(accountsObj));

      if (withManagedAccess) {
          return gs.nil(managedAccessAccounts) ? [] : Object.keys(managedAccessAccounts);
      }

      var _accounts = Object.keys(managedAccessAccounts);

      for (var i = 0, l = _accounts.length; i < l; i++) {
          delete accountsObj[_accounts[i]];
      }

      return Object.keys(accountsObj);
  },


  isRestrictedAccessAccount: function(accountId) {
      if (!accountId)
          return false;

      var gr = new GlideRecord(global.CSMBaseConstants.ACCOUNT_ACCESS_TABLE);
      gr.addQuery('account', accountId);
      gr.addQuery('restrict_contact_access', true);
      gr.setLimit(1);
      gr.query();
      return gr.hasNext();
  },

  _getMyAccountsAsObject: function(countMe, getContactRelationships, getCustomerAccounts) {
      var accounts = {};

      var customerAccounts = getCustomerAccounts ? this.getAccountsFromAccountRelationship() : [];
      var additionalAccounts = getContactRelationships ? this.getAccountsFromContactRelationship() : [];

      function collectAccounts(_accounts) {
          for (var i = 0; i < _accounts.length; i++) {
              accounts[_accounts[i]] = true;
          }
      }

      collectAccounts(customerAccounts);
      collectAccounts(additionalAccounts);
      if (countMe && this.getUserAccount()) accounts[this.getUserAccount()] = true;

      return accounts;
  },

  _getAccountsWithManageAccess: function(accounts) {
      return this.accountDao.getAccountsWithManageAccess(accounts);
  },
  
  /*
   * Using GlideProperties.get() because gs.getProperty() is not supported for client callable script includes.
   */
  _isPrivateCachingEnabledForAccounts: function() {
  	return (GlideProperties.getBoolean("use_accounts_private_cache", false));
  },
  
  type: 'AccountImpl'
};

Sys ID

becbb0f75312030097a2ddeeff7b12f3

Offical Documentation

Official Docs: