Certainly! When preparing for an interview where background scripting in ServiceNow is a topic, it's essential to understand how to use the GlideRecord class, which is the primary way to interact with the database in scripts.
Here are some common scripting tasks that could be relevant for a 3-year experience candidate, along with example scripts for each scenario:
1. Print Last 5 Incidents
You can retrieve the latest 5 incidents by ordering them by the sys_created_on field in descending order. Here is how you might script that:
var gr = new GlideRecord('incident');
gr.orderByDesc('sys_created_on');
gr.setLimit(5); // Limit to 5 records
gr.query();
while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Created on: ' + gr.sys_created_on);
}
2. Print All Incidents Where Caller is Inactive
To find incidents where the caller (caller_id) is inactive, you can check the sys_user table for inactive users. Here’s an example:
var inactiveUsers = new GlideRecord('sys_user');
inactiveUsers.addQuery('active', false);
inactiveUsers.query();
var inactiveUserIDs = [];
while (inactiveUsers.next()) {
inactiveUserIDs.push(inactiveUsers.sys_id.toString());
}
var gr = new GlideRecord('incident');
gr.addQuery('caller_id', 'IN', inactiveUserIDs.join(','));
gr.query();
while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Caller: ' + gr.caller_id.getDisplayValue());
}
3. Update Incidents with a Specific State
You might be asked how to update incidents with a specific state, for example, closing all completed incidents. Here's how you would do that:
var gr = new GlideRecord('incident');
gr.addQuery('state', '7'); // Assuming state 7 is 'Resolved'
gr.query();
while (gr.next()) {
gr.state = '6'; // Assuming state 6 is 'Closed'
gr.update();
gs.info('Closed Incident: ' + gr.number);
}
4. Count the Number of Open Incidents
This script counts the number of open incidents:
var gr = new GlideRecord('incident');
gr.addQuery('active', true); // or the specific states for open incidents
gr.query();
gs.info('Total Open Incidents: ' + gr.getRowCount());
5. Retrieve and Print Incidents Created Last 7 Days
To get incidents created in the last week:
var gr = new GlideRecord('incident');
gr.addQuery('sys_created_on', '>=', gs.daysAgo(7)); // Last 7 days
gr.query();
while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Created on: ' + gr.sys_created_on);
}
6. Delete Incidents with a Specific Condition
You might be asked about deleting incidents based on a certain condition, such as those created more than a year ago:
var gr = new GlideRecord('incident');
gr.addQuery('sys_created_on', '<=', gs.daysAgo(365)); // More than a year old
gr.query();
while (gr.next()) {
gs.info('Deleting Incident: ' + gr.number);
gr.deleteRecord(); // Be cautious with this
}
7. Add a Comment to All Incidents
You might be asked to add a comment to all incidents for a specific reason:
var gr = new GlideRecord('incident');
gr.query();
while (gr.next()) {
gr.comments = "This incident has been reviewed.";
gr.update();
gs.info('Updated Incident: ' + gr.number + ' with new comment.');
}
8. Find Incidents Assigned to a Specific User
To find all incidents assigned to a specific user (replace USER_SYS_ID with actual sys_id):
var gr = new GlideRecord('incident');
gr.addQuery('assigned_to', 'USER_SYS_ID'); // Replace with actual sys_id
gr.query();
while (gr.next()) {
gs.info('Incident Number: ' + gr.number + ', Assigned to: ' + gr.assigned_to.getDisplayValue());
}
Summary
These script snippets cover common scenarios you may encounter in a ServiceNow background script context for incidents. Be prepared to explain how the GlideRecord queries work, as well as the potential impacts of the scripts you would write, especially those that modify or delete records. Always test scripts in a safe environment where you won't unintentionally disrupt live data.