|
1 | | -Convert Active Incidents to JSON – ServiceNow |
2 | | -Overview |
| 1 | +# Active Incidents JSON Export – ServiceNow |
3 | 2 |
|
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. |
| 3 | +This repository contains **two approaches** to fetch active incidents from ServiceNow and convert them to JSON. Both use `GlideRecord` but differ in flexibility and readability. |
5 | 4 |
|
6 | | -By converting records to JSON, you can quickly share structured data with external systems, automate processes, or use it in scripts and dashboards. |
| 5 | +--- |
7 | 6 |
|
8 | | -Features |
| 7 | +## 1. Simple Approach |
9 | 8 |
|
10 | | -Retrieves all active incidents from the incident table. |
| 9 | +This method fetches a **fixed set of fields** and converts them directly to JSON. |
11 | 10 |
|
12 | | -Dynamically extracts selected fields (configurable). |
| 11 | +```javascript |
| 12 | +var incidents = []; |
| 13 | +var gr = new GlideRecord('incident'); |
| 14 | +gr.addQuery('active', true); |
| 15 | +gr.query(); |
| 16 | +while (gr.next()) { |
| 17 | + incidents.push({ |
| 18 | + number: gr.number.toString(), |
| 19 | + short_description: gr.short_description.toString(), |
| 20 | + state: gr.state.toString(), |
| 21 | + assigned_to: gr.assigned_to.getDisplayValue('name'), |
| 22 | + created_on: gr.sys_created_on.getDisplayValue() |
| 23 | + }); |
| 24 | +} |
13 | 25 |
|
14 | | -Automatically resolves reference fields to display values. |
| 26 | +var jsonOutput = JSON.stringify(incidents); |
| 27 | +gs.info(jsonOutput); |
| 28 | +``` |
15 | 29 |
|
16 | | -Adds human-readable state labels (e.g., "New", "In Progress", "Resolved"). |
| 30 | +### ✅ Pros |
| 31 | +- Simple and easy to implement |
| 32 | +- Works fine for a fixed set of fields |
| 33 | +- Direct JSON output |
17 | 34 |
|
18 | | -Outputs pretty-printed JSON for easy readability. |
| 35 | +### ❌ Cons |
| 36 | +- Fields are hard-coded → not reusable for other tables |
| 37 | +- Reference fields handling is not dynamic |
| 38 | +- Numeric state values are not human-readable |
19 | 39 |
|
20 | | -Configuration |
| 40 | +--- |
21 | 41 |
|
22 | | -Table Name: Set the tableName variable to the table you want to extract records from. |
| 42 | +## 2. Flexible & Dynamic Approach |
23 | 43 |
|
24 | | -Fields to Include: Update the fieldsToInclude array to include the fields you need in the JSON. For example: |
| 44 | +This method allows dynamic fields, handles reference fields, and adds human-readable state labels. |
| 45 | + |
| 46 | +```javascript |
| 47 | +var tableName = 'incident'; |
| 48 | +var fieldsToInclude = ['number', 'short_description', 'state', 'assigned_to', 'sys_created_on']; |
| 49 | +var incidentList = []; |
| 50 | + |
| 51 | +var gr = new GlideRecord(tableName); |
| 52 | +gr.addQuery('active', true); |
| 53 | +gr.query(); |
| 54 | + |
| 55 | +while (gr.next()) { |
| 56 | + var incidentObj = {}; |
| 57 | + |
| 58 | + fieldsToInclude.forEach(function(field) { |
| 59 | + if (gr.isValidField(field) && gr[field].getRefRecord) { |
| 60 | + incidentObj[field] = gr[field].getDisplayValue(); |
| 61 | + } else if (gr.isValidField(field)) { |
| 62 | + incidentObj[field] = gr[field].toString(); |
| 63 | + } else { |
| 64 | + incidentObj[field] = null; |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + // Map numeric state to human-readable label |
| 69 | + var stateMap = { |
| 70 | + '1': 'New', |
| 71 | + '2': 'In Progress', |
| 72 | + '3': 'On Hold', |
| 73 | + '6': 'Resolved', |
| 74 | + '7': 'Closed' |
| 75 | + }; |
| 76 | + incidentObj.state_label = stateMap[incidentObj.state] || 'Unknown'; |
| 77 | + |
| 78 | + incidentList.push(incidentObj); |
| 79 | +} |
| 80 | + |
| 81 | +var jsonOutput = JSON.stringify(incidentList, null, 2); |
| 82 | +gs.info("Here's your JSON for active incidents:\n" + jsonOutput); |
| 83 | +``` |
| 84 | + |
| 85 | +### ✅ Pros |
| 86 | +- Dynamic → easily reusable for any table and fields |
| 87 | +- Handles reference fields gracefully |
| 88 | +- Adds human-readable state labels |
| 89 | +- JSON is formatted for readability |
| 90 | +- Checks `isValidField` to prevent errors |
| 91 | + |
| 92 | +### ❌ Cons |
| 93 | +- Slightly more complex than the simple version |
| 94 | +- Requires manual mapping for fields like state labels |
| 95 | + |
| 96 | + |
| 97 | +## Summary |
| 98 | + |
| 99 | +- **Simple Approach**: Best for quick tasks with fixed fields |
| 100 | +- **Flexible Approach**: Best for reusable scripts, handling dynamic tables, reference fields, and human-readable output |
0 commit comments