Skip to content

Commit 313765d

Browse files
Create convert incidents to JSON.js
1 parent ffe114b commit 313765d

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
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)