Skip to Content

GlideAjax Response Handler

Provide server-side functions that can be called from Client Scripts via GlideAjax.

#glideajax #ajax #client-callable #user-info #metrics

Script Code

JavaScript
1var UserInfoUtils = Class.create();
2UserInfoUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
3
4  /**
5   * Get the count of open tickets for a user
6   * Called from client via GlideAjax
7   *
8   * Parameters expected:
9   *   sysparm_user_id - sys_id of the user
10   *
11   * Returns: Number of open incidents (string)
12   */
13  getUserOpenTickets: function() {
14    // Get the user ID parameter from client
15    var userId = this.getParameter('sysparm_user_id');
16
17    // Validate input
18    if (!userId) {
19      return '0';
20    }
21
22    // Query for open incidents
23    var grIncident = new GlideAggregate('incident');
24    grIncident.addQuery('caller_id', userId);
25    grIncident.addQuery('active', 'true');
26    grIncident.addAggregate('COUNT');
27    grIncident.query();
28
29    var count = 0;
30    if (grIncident.next()) {
31      count = grIncident.getAggregate('COUNT');
32    }
33
34    // Return string value (required for GlideAjax)
35    return count.toString();
36  },
37
38  /**
39   * Get detailed user information including tickets and metrics
40   *
41   * Parameters expected:
42   *   sysparm_user_id - sys_id of the user
43   *
44   * Returns: JSON string with user details
45   */
46  getUserDetails: function() {
47    var userId = this.getParameter('sysparm_user_id');
48
49    if (!userId) {
50      return JSON.stringify({error: 'User ID required'});
51    }
52
53    // Get user record
54    var grUser = new GlideRecord('sys_user');
55    if (!grUser.get(userId)) {
56      return JSON.stringify({error: 'User not found'});
57    }
58
59    // Build response object
60    var userDetails = {
61      email: grUser.email.toString(),
62      phone: grUser.phone.toString(),
63      location: grUser.location.toString(),
64      department: grUser.department.getDisplayValue(),
65      manager: grUser.manager.getDisplayValue(),
66      vip: grUser.vip.toString() === 'true',
67      openTickets: this._getOpenTicketCount(userId),
68      avgResolutionTime: this._getAvgResolutionTime(userId)
69    };
70
71    // Return JSON string
72    return JSON.stringify(userDetails);
73  },
74
75  /**
76   * Private helper: Get count of open tickets for user
77   * @param {string} userId - sys_id of user
78   * @returns {number} count of open tickets
79   */
80  _getOpenTicketCount: function(userId) {
81    var ga = new GlideAggregate('incident');
82    ga.addQuery('caller_id', userId);
83    ga.addQuery('active', 'true');
84    ga.addAggregate('COUNT');
85    ga.query();
86
87    if (ga.next()) {
88      return parseInt(ga.getAggregate('COUNT'));
89    }
90    return 0;
91  },
92
93  /**
94   * Private helper: Calculate average resolution time in days
95   * @param {string} userId - sys_id of user
96   * @returns {string} average days formatted to 1 decimal
97   */
98  _getAvgResolutionTime: function(userId) {
99    var ga = new GlideAggregate('incident');
100    ga.addQuery('caller_id', userId);
101    ga.addQuery('closed_at', '!=', '');
102    ga.addQuery('sys_created_on', '>', gs.daysAgoStart(90));  // Last 90 days
103    ga.addAggregate('AVG', 'calendar_duration');
104    ga.query();
105
106    if (ga.next()) {
107      var avgDuration = ga.getAggregate('AVG', 'calendar_duration');
108      var avgDays = avgDuration / (24 * 60 * 60);  // Convert seconds to days
109      return avgDays.toFixed(1);
110    }
111    return '0.0';
112  },
113
114  type: 'UserInfoUtils'
115});

How to Use

1. Create a new Script Include 2. Set Name to "UserInfoUtils" (or your chosen name) 3. Check "Client callable" checkbox 4. Set "Extends" to "AbstractAjaxProcessor" 5. Copy the code above 6. Use from Client Scripts as shown in the GlideAjax examples above

Explore More Scripts

Browse our complete library of ServiceNow scripts

View All Scripts