From 313765d5bd18e5756158c5d439bcbd0ea62134c3 Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Mon, 20 Oct 2025 23:44:13 +0530 Subject: [PATCH 1/2] Create convert incidents to JSON.js --- .../convert incidents to JSON.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Convert Incident Records to JSON/convert incidents to JSON.js diff --git a/Server-Side Components/Background Scripts/Convert Incident Records to JSON/convert incidents to JSON.js b/Server-Side Components/Background Scripts/Convert Incident Records to JSON/convert incidents to JSON.js new file mode 100644 index 0000000000..920c50074e --- /dev/null +++ b/Server-Side Components/Background Scripts/Convert Incident Records to JSON/convert incidents to JSON.js @@ -0,0 +1,45 @@ +// Table and fields you want to include +var tableName = 'incident'; // Change to any table you like +var fieldsToInclude = ['number', 'short_description', 'state', 'assigned_to', 'sys_created_on'];// Change whichever field you wish for + +// Store all incident data here +var incidentList = []; + +// Get active incidents +var gr = new GlideRecord(tableName); +gr.addQuery('active', true); +gr.query(); + +// Go through each record and build a friendly object +while (gr.next()) { + var incidentObj = {}; + + fieldsToInclude.forEach(function(field) { + if (gr.isValidField(field) && gr[field].getRefRecord) { + // Get display value for reference fields + incidentObj[field] = gr[field].getDisplayValue(); + } else if (gr.isValidField(field)) { + incidentObj[field] = gr[field].toString(); + } else { + incidentObj[field] = null; + } + }); + + // Add human-readable state + var stateMap = { + '1': 'New', + '2': 'In Progress', + '3': 'On Hold', + '6': 'Resolved', + '7': 'Closed' + }; + incidentObj.state_label = stateMap[incidentObj.state] || 'Unknown'; + + incidentList.push(incidentObj); +} + +// Convert the list to JSON in a readable format +var jsonOutput = JSON.stringify(incidentList, null, 2); + +// Show the JSON output in system logs +gs.info("Here’s your JSON for active incidents:\n" + jsonOutput); From 2931511e01af8e303625fc20d7ea6da49d0d60d9 Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Mon, 20 Oct 2025 23:45:21 +0530 Subject: [PATCH 2/2] Create Readme.md --- .../Readme.md | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 Server-Side Components/Background Scripts/Convert Incident Records to JSON/Readme.md diff --git a/Server-Side Components/Background Scripts/Convert Incident Records to JSON/Readme.md b/Server-Side Components/Background Scripts/Convert Incident Records to JSON/Readme.md new file mode 100644 index 0000000000..9a4b8ad5d5 --- /dev/null +++ b/Server-Side Components/Background Scripts/Convert Incident Records to JSON/Readme.md @@ -0,0 +1,24 @@ +Convert Active Incidents to JSON – ServiceNow +Overview + +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. + +By converting records to JSON, you can quickly share structured data with external systems, automate processes, or use it in scripts and dashboards. + +Features + +Retrieves all active incidents from the incident table. + +Dynamically extracts selected fields (configurable). + +Automatically resolves reference fields to display values. + +Adds human-readable state labels (e.g., "New", "In Progress", "Resolved"). + +Outputs pretty-printed JSON for easy readability. + +Configuration + +Table Name: Set the tableName variable to the table you want to extract records from. + +Fields to Include: Update the fieldsToInclude array to include the fields you need in the JSON. For example: