var userID = gs.getUserID(); var incident = new GlideAggregate('incident');…
- User
var userID = gs.getUserID(); var incident = new GlideAggregate('incident'); incident.addQuery('sys_created_by', userID); incident.addAggregate('COUNT'); incident.query();
var totalCount = 0; while(incident.next()){ totalCount = addAggregate('COUNT'); }fix this
- Assistant
It looks like you're attempting to obtain a count of incident records created by the current user in a ServiceNow instance using a GlideAggregate. There are a few issues in your script which could be preventing it from working correctly.
Here is a revised version of the script with fixes and explanations:
var userID = gs.getUserID(); // Get the current user's ID var incident = new GlideAggregate('incident'); // Create a new GlideAggregate for the incident table incident.addQuery('sys_created_by', userID); // Add a query to filter incidents created by the user incident.addAggregate('COUNT'); // Add an aggregate function to count the incidents incident.query(); // Issue the query against the database var totalCount = 0; if (incident.next()) { totalCount = incident.getAggregate('COUNT'); // Get the count result from the aggregate } gs.info("Total incidents created by user: " + totalCount); // Print the count to the log - User
write a business rule script to calculate the total incident created by logged in user in servicenow
- Assistant
- User
from before business rule
- Assistant
- User
give background script code
- Assistant
If you want to run a one-time script to calculate the total number of incidents created by the currently logged-in user in ServiceNow, you can use a background script. Background scripts run server-side JavaScript in a ServiceNow instance, allowing administrators to execute scripts for maintenance, data manipulation, or to test scripts that will be used in Business Rules or other server-side scripting areas.
Below is an example of how you would write a background script to count the total number of incident records created by the logged-in user:
// Get the current user's ID var userID = gs.getUserID(); // Create a new GlideAggregate object for the incident table var incidentAgg = new GlideAggregate('incident'); // Add a query to filter incidents created by the user incidentAgg.addQuery('sys_created_by', userID); // Add an aggregate function to count the incidents incidentAgg.addAggregate('COUNT'); // Issue the query against the database incidentAgg.query(); var totalCount = 0; // Get the count from the query result if (incidentAgg.next()) { totalCount = incidentAgg.getAggregate('COUNT'); } // Output the total count gs.info('Total incidents created by user ' + userID + ': ' + totalCount); - User
using glide record
- Assistant
If you intend to count total incidents created by the currently logged-in user using
GlideRecordin a background script, you can do so by iterating over the incidents and keeping a tally. However, to improve performance, it's still recommended to use for counting records as it is much more efficient and doesn't require loading all the incident records into memory.