Name

sn_app_eng_studio.ApplicationService

Description

App Engine Studio s interaction layer for applications

Script

var ApplicationService = Class.create();

ApplicationService.prototype = {
  initialize: function() {},

  _addSearchQuery: function(gr, searchTerm) {
      var query = gr.addQuery('name', 'CONTAINS', searchTerm);
      query.addOrCondition('short_description', 'CONTAINS', searchTerm);
  },

  getAccessibleApps: function() {
      return gs.getSession().getApplicationPickerList();
  },

  getAccessibleAppSysIds: function() {
      return this.getAccessibleApps().map(function(app) {
          return app.sysId;
      });
  },

  /**
   * @return {object} object of sys_ids of apps the user has opened in AES against time it was last opened
   */
  getUserAppsLastOpened: function(args) {
      args = args || {};
      var searchTerm = args.searchTerm;
      var currentUserId = gs.getUserID();
      var appsLastOpenedPref = new GlideRecord('sys_user_preference');
      var existingAppRecords = new GlideRecordSecure('sys_app');
      var queryAppSysIds = [];
      var lastOpenedApps = {};

      appsLastOpenedPref.addQuery('name', 'sn_app_eng_studio.last_opened');
      appsLastOpenedPref.addQuery('user', currentUserId);
      appsLastOpenedPref.query();

      if (appsLastOpenedPref.hasNext()) {
          appsLastOpenedPref.next();
          var pref = appsLastOpenedPref.getValue('value');
          var userLastOpenedAppsLookup = {};

          try {
              userLastOpenedAppsLookup = JSON.parse(pref);

              // exclude apps that no longer exist
              for (var key in userLastOpenedAppsLookup) {
                  queryAppSysIds.push(key);
              }

              existingAppRecords.addQuery('sys_id', 'IN', queryAppSysIds.join(','));

              if (searchTerm) {
                  this._addSearchQuery(existingAppRecords, searchTerm);
              }
              existingAppRecords.query();

              while (existingAppRecords.next()) {
                  var currentSysId = existingAppRecords.getValue('sys_id');
                  lastOpenedApps[currentSysId] = userLastOpenedAppsLookup[currentSysId];
              }

          } catch (ex) {
              gs.error('Unable to parse sys_user_preference for sn_app_eng_studio.last_opened. Exception: ' + ex);
          }
      }

      return lastOpenedApps;
  },

  /** 
   * @param {Object} args - The argument object
   * @param {string} args.sortField - field in sys_app table to sort by
   * @param {string} args.sortOrder - sort direction    
   * @param {number} args.offset - starting record index (set to null to retrieve all apps)
   * @param {number} args.limit - how many results to return (set to null to retrieve all apps)
   * @param {array} args.excludedAppSysIds - app sys_ids to exclude from glide query
   * @param {string} args.searchTerm - term to filter the application results by
   * @return {array} array of sys_ids of sys_scope records for applications user can read 
   */
  getUserApplications: function(args) {
      args = args || {};
      var sortField = args.sortField;
      var sortOrder = args.sortOrder;
      var offset = args.offset;
      var limit = args.limit;
      var excludedAppSysIds = args.excludedAppSysIds;
      var searchTerm = args.searchTerm;

      var userApps = [];
      var hasMore = false;
      var apps = new GlideRecordSecure('sys_app');
      var index = 0;

      if (sortOrder === CreatorStudioConstants.sorting.ASC_ORDER) {
          apps.orderBy(sortField);
      } else {
          apps.orderByDesc(sortField);
      }

      // exclude sys_app records in global scope
      apps.addEncodedQuery('scope!=' + CreatorStudioConstants.GLOBAL_APPLICATION_SCOPE_NAME);

      // exclude sys_ids passed in
      apps.addQuery('sys_id', 'NOT IN', excludedAppSysIds.join(','));

      // check for searchTerm
      if (searchTerm) {
          this._addSearchQuery(apps, searchTerm);
      }

      // query for all apps
      apps.query();

      if (limit !== null && offset !== null) {
          while (apps.next() && userApps.length < limit) {
              if (index >= offset) {
                  userApps.push(apps.getValue('sys_id'));
              }

              index++;
          }

          hasMore = apps.hasNext();

      } else {
          while (apps.next()) {
              userApps.push(apps.getValue('sys_id'));
          }
      }

      return {
          applications: userApps,
          hasMore: hasMore,
      };
  },

  type: 'ApplicationService'
};

Sys ID

137412d75b32401083f30f216581c718

Offical Documentation

Official Docs: