in servicenow, // print the name of caller , and number of incident opened for them if…
- User
in servicenow, // print the name of caller , and number of incident opened for them if the total number of incident is more than 5.
- Assistant
To achieve your goal in ServiceNow, you can write a script (e.g., a background script) that queries the Incident table and counts the number of incidents opened by each caller. If the number of incidents for a particular caller exceeds 5, you can print their name along with the count. Here’s how you can do that:
// Create a GlideRecord object for the Incident table var incidentGR = new GlideRecord('incident'); // Use a GlideAggregate to count the incidents for each caller var agg = new GlideAggregate('incident'); agg.addAggregate('COUNT'); agg.addGroupBy('caller_id'); agg.query(); while (agg.next()) { var callerId = agg.getValue('caller_id'); // Get the caller ID var incidentCount = agg.getAggregate('COUNT'); // Get the count of incidents // Check if the count exceeds 5 if (incidentCount > 5) { var callerGR = new GlideRecord('sys_user'); // Create a GlideRecord for the User table if (callerGR.get(callerId)) { // Load the caller record var callerName = callerGR.getValue('name'); // Get the caller's name // Print the caller's name and the incident count gs.info('Caller: ' + callerName + ', Incidents Opened: ' + incidentCount); } } }Explanation of the Script:
- GlideAggregate: This is used to count incidents grouped by the caller.
- addAggregate('COUNT'): This function counts the number of incidents.
- addGroupBy('caller_id'): This groups the results by the caller ID.
- Fetch and Print: For each caller with more than 5 incidents, the script fetches the caller’s name from the
sys_usertable and prints it along with the number of incidents.
You can run this script in the Scripts - Background (found in the application navigator under System Definition) module of ServiceNow.
Note: This script assumes that you have access to the required tables and that the field names (
caller_id,name) are standard. If your instance has custom fields or configurations, you may need to adjust the script accordingly. - User
use glide reecord
- Assistant
Certainly! If you want to perform the same operation using
GlideRecord, without usingGlideAggregate, you can fetch all incidents and count them for each caller manually. Here's how to do that:// Create a GlideRecord object for the Incident table var incidentGR = new GlideRecord('incident'); // Create an object to store the count of incidents per caller var callerIncidentCount = {}; // Query all incidents incidentGR.query(); // Iterate through each incident while (incidentGR.next()) { var callerId = incidentGR.caller_id; // Get the caller ID // Initialize the count if the caller ID doesn't exist in the object if (callerIncidentCount[callerId]) { callerIncidentCount[callerId]++; } else { callerIncidentCount[callerId] = 1; } } // Now loop through the collected counts and print the names of callers with more than 5 incidents for (var id in callerIncidentCount) { if (callerIncidentCount.hasOwnProperty(id) && callerIncidentCount[id] > 5) { var callerGR = new GlideRecord('sys_user'); // Create a GlideRecord for the User table if (callerGR.get(id)) { // Load the caller record var callerName = callerGR.getValue('name'); // Get the caller's name // Print the caller's name and the count of incidents opened gs.info('Caller: ' + callerName + ', Incidents Opened: ' + callerIncidentCount[id]); } } }Explanation of the Script:
-
Initialize
callerIncidentCountObject: This object will hold the count of incidents for each caller ID. -
Query All Incidents: The
GlideRecordfor the Incident table is queried to loop through each incident. -
Count Incidents: For each incident, we check if the caller ID already exists in the
callerIncidentCountobject. If it does, we increment the count; if it doesn't, we set the initial count to 1. -
Loop Through Counts: We then loop through the
callerIncidentCountobject to find callers with more than 5 incidents. -
Fetch Caller Name: We use
GlideRecordto fetch the caller’s name from thesys_usertable based on the caller ID and print the results.
You can run this script in the "Scripts - Background" module in your ServiceNow instance to get the desired output. This approach guarantees that we use
GlideRecordto perform the task without aggregations. -