Skip to content

Commit 8859b81

Browse files
Convert incident records to json (#2351)
* Create convert incidents to JSON.js * Create Readme.md
1 parent ffe114b commit 8859b81

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Convert Active Incidents to JSON – ServiceNow
2+
Overview
3+
4+
This script fetches all active Incident records from your ServiceNow instance and converts them into a JSON format. The JSON output is easy to read and can be used for reporting, integration, or debugging purposes.
5+
6+
By converting records to JSON, you can quickly share structured data with external systems, automate processes, or use it in scripts and dashboards.
7+
8+
Features
9+
10+
Retrieves all active incidents from the incident table.
11+
12+
Dynamically extracts selected fields (configurable).
13+
14+
Automatically resolves reference fields to display values.
15+
16+
Adds human-readable state labels (e.g., "New", "In Progress", "Resolved").
17+
18+
Outputs pretty-printed JSON for easy readability.
19+
20+
Configuration
21+
22+
Table Name: Set the tableName variable to the table you want to extract records from.
23+
24+
Fields to Include: Update the fieldsToInclude array to include the fields you need in the JSON. For example:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Table and fields you want to include
2+
var tableName = 'incident'; // Change to any table you like
3+
var fieldsToInclude = ['number', 'short_description', 'state', 'assigned_to', 'sys_created_on'];// Change whichever field you wish for
4+
5+
// Store all incident data here
6+
var incidentList = [];
7+
8+
// Get active incidents
9+
var gr = new GlideRecord(tableName);
10+
gr.addQuery('active', true);
11+
gr.query();
12+
13+
// Go through each record and build a friendly object
14+
while (gr.next()) {
15+
var incidentObj = {};
16+
17+
fieldsToInclude.forEach(function(field) {
18+
if (gr.isValidField(field) && gr[field].getRefRecord) {
19+
// Get display value for reference fields
20+
incidentObj[field] = gr[field].getDisplayValue();
21+
} else if (gr.isValidField(field)) {
22+
incidentObj[field] = gr[field].toString();
23+
} else {
24+
incidentObj[field] = null;
25+
}
26+
});
27+
28+
// Add human-readable state
29+
var stateMap = {
30+
'1': 'New',
31+
'2': 'In Progress',
32+
'3': 'On Hold',
33+
'6': 'Resolved',
34+
'7': 'Closed'
35+
};
36+
incidentObj.state_label = stateMap[incidentObj.state] || 'Unknown';
37+
38+
incidentList.push(incidentObj);
39+
}
40+
41+
// Convert the list to JSON in a readable format
42+
var jsonOutput = JSON.stringify(incidentList, null, 2);
43+
44+
// Show the JSON output in system logs
45+
gs.info("Here’s your JSON for active incidents:\n" + jsonOutput);

0 commit comments

Comments
 (0)