Print all incident whose caller is inactive in background script servicenow
- User
Print all incident whose caller is inactive in background script servicenow
- Assistant
In ServiceNow, if you want to print all incidents where the caller is inactive using a background script, you can accomplish this by querying the
incidenttable and thesys_usertable. You will need to check for active users, and then find incidents associated with inactive users. Here's a sample script you can run in the Scripts - Background module:javascript// Create a GlideRecord object for the sys_user table to find inactive users var userGR = new GlideRecord('sys_user'); userGR.addQuery('active', false); // Look for inactive users userGR.query(); var inactiveUsers = []; while (userGR.next()) { inactiveUsers.push(userGR.sys_id.toString()); } // If we found inactive users, proceed to find their incidents if (inactiveUsers.length > 0) { // Create a GlideRecord object for the incident table var incidentGR = new GlideRecord('incident'); incidentGR.addQuery('caller_id', 'IN', inactiveUsers); incidentGR.query(); // Print the incidents where the caller is inactive while (incidentGR.next()) { gs.print('Incident Number: ' + incidentGR.number + ', Caller: ' + incidentGR.caller_id.getDisplayValue()); } } else { gs.print('No inactive users found.'); }Explanation:
-
User Query: You start by querying the
sys_usertable for users who are inactive. Store theirsys_idvalues in an array since we'll need to match them against the incidents. -
Incident Query: Next, query the
incidenttable for incidents where thecaller_idis within the list of inactive user IDs you collected. -
Output: Iterate through the retrieved incidents and print out details such as the incident number and the name of the caller for each incident.
Ensure you have the necessary permissions to run scripts in the Script - Background and be aware that executing scripts directly in production can have implications. It's generally a good idea to test scripts in a non-production environment first.
-