Skip to content
AbstractAjaxProcessor Existing-Request Check

AbstractAjaxProcessor Existing-Request Check

Overview

This pattern is a building block for Just-In-Time (JIT) permission requests. These are catalog items that grant temporary, time-limited access and are meant to only ever have one active grant per user at a time. Before the user finishes filling out a new request, it warns them if they already have an active RITM for that same catalog item, so they understand a new submission will override the existing one rather than stack on top of it.

The pattern has two halves:

  1. A Script Include extending AbstractAjaxProcessor (marked Client Callable) that queries sc_req_item for an active request on a specific catalog item (cat_item) submitted by the current user (requested_for), then looks up a variable’s stored value (like end_time) via the sc_item_option_mtom join table. It returns a small JSON payload describing whether a request exists and its relevant details.
  2. An onLoad Catalog Client Script that calls the Script Include via GlideAjax, and if exists comes back true, shows an informational field message warning the user.

A closely related variant extends the same idea to a capacity check rather than a simple existence check: instead of just detecting one active request, it counts how many active requests exist system-wide for the catalog item, and reports max_reached = true once two or more exist. along with the two most recent end_time values, sorted chronologically and joined with “and” for display. This is useful when the restriction is a shared pool limit rather than one-per-user.

Problem Statement

Any catalog item used to grant time-limited access needs a way to enforce one active grant at a time, or a shared limit when access has a limited capacity. Without this check, a user who already has active access could submit another request and unintentionally change their existing expiration time without realizing it.

Showing a warning before the user completes the request helps prevent confusion about why their access may end earlier or later than expected.

Since multiple catalog items need to perform the same sc_req_item and sc_item_option_mtom lookup, that logic is handled in a single Script Include and reused across items. Each catalog item simply passes its own hardcoded catalog item and variable sys_id.

CheckExistingJitRequest.js
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * Script Name: CheckExistingJitRequest
 * Description: This Script Include checks if the current user has an active JIT (Just-In-Time) access request (RITM) for a specific catalog item.
 *              If one exists, it retrieves the value of the `end_time` variable from the associated request item.
 *              This script is used to inform users in the Service Portal that their existing access may be overridden.
 * 
 * Usage: This script is used as a server-side processor callable from a GlideAjax call in a Catalog Client Script.
 *      Example record:
 *         Name: CheckExistingJitRequest
 *         Table: sys_script_include (Script Include) 
 * 
 * Context: Called from an `onLoad` Catalog Client Script on a JIT-access catalog item form. 
 *          It uses GlideRecord to search the `sc_req_item` table for active requests created for the logged-in user and retrieves the `end_time` from the `sc_item_option_mtom` table.
 * 
 * Author: Konner Lester
 * Date Created: May 23, 2025
 * Last Modified: May 23, 2025
 * 
 * Note: 
 * - This Script Include must be marked as **Client Callable**.
 * - The `catItemId` and `endTimeVariableId` values are hardcoded and must match the catalog item and variable sys_ids.
 * - The `end_time` variable must exist on the catalog item and be correctly referenced in `sc_item_option_mtom`.
 */

var CheckExistingJitRequest = Class.create();
CheckExistingJitRequest.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

  checkExisting: function() {
      var userId = gs.getUserID();
      var catItemId = '23ac29d33bad2610fd6edba693e45ab6'; // Catalog item sys_id
      var endTimeVariableId = '2d1ef2373b256a10fd6edba693e45aa9'; // Variable sys_id for end_time

      var result = {
          exists: false,
          end_time: ''
      };

      var ritmGr = new GlideRecord('sc_req_item');
      ritmGr.addQuery('active', true);
      ritmGr.addQuery('cat_item', catItemId);
      ritmGr.addQuery('requested_for', userId);
      ritmGr.orderByDesc('sys_created_on'); // Just in case there are multiple
      ritmGr.query();

      if (ritmGr.next()) {
          result.exists = true;

          // Look up the associated variable value
          var mtomGr = new GlideRecord('sc_item_option_mtom');
          mtomGr.addQuery('request_item', ritmGr.sys_id);
          mtomGr.addQuery('sc_item_option.item_option_new', endTimeVariableId);
          mtomGr.query();

          if (mtomGr.next()) {
              result.end_time = mtomGr.sc_item_option.value + ''; // Convert to string
          }
      }

      return new JSON().encode(result);
  }
});
Last updated on