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/4] 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/4] 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: From 0db26eea730667065c237ba4f347a096b4f7088f Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Tue, 21 Oct 2025 00:23:28 +0530 Subject: [PATCH 3/4] Create Readme.md --- .../findTableSize/Readme.md | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Server-Side Components/Background Scripts/findTableSize/Readme.md diff --git a/Server-Side Components/Background Scripts/findTableSize/Readme.md b/Server-Side Components/Background Scripts/findTableSize/Readme.md new file mode 100644 index 0000000000..1b00004507 --- /dev/null +++ b/Server-Side Components/Background Scripts/findTableSize/Readme.md @@ -0,0 +1,48 @@ +Table Size Retriever – ServiceNow +Overview + +This script retrieves the size of any table in your ServiceNow instance in gigabytes and provides a fallback estimate if metadata is not available. It also logs the total number of records in the table. + +This is useful for instance management, capacity planning, and gaining insights into table sizes without manually inspecting each table. + +Features + +Retrieves the official table size from the sys_physical_table_stats table if available. + +Falls back to a size estimate based on record count when metadata is missing. + +Logs the number of records in the table. + +Can be used with any table by changing the tableName variable. + +Fully compatible with Background Scripts. + +Configuration + +Table Name: Set the tableName variable to the table you want to check. + +var tableName = 'task'; // Replace with any table name + +How It Works + +Queries the sys_physical_table_stats table for the specified table name. + +If the table exists in metadata, retrieves the size in gigabytes. + +If metadata is not available, estimates the table size based on record count (assuming 1 KB per record). + +Logs the table size (official or estimated) in GB. + +Separately retrieves and logs the total number of records in the table. + +Usage + +Navigate to System Definition → Scripts – Background in your ServiceNow instance. + +Copy and paste the script. + +Update the tableName variable as needed. + +Click Run Script. + +Check the system logs to view the table size and record count. From 6d0f9424639d23e197411a869aea3537540621e2 Mon Sep 17 00:00:00 2001 From: Sachin Narayanasamy <132642563+SachinNarayanasamy@users.noreply.github.com> Date: Tue, 21 Oct 2025 00:25:06 +0530 Subject: [PATCH 4/4] Create Find Table Size.js --- .../findTableSize/Find Table Size.js | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 Server-Side Components/Background Scripts/findTableSize/Find Table Size.js diff --git a/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js b/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js new file mode 100644 index 0000000000..40b1adc7d4 --- /dev/null +++ b/Server-Side Components/Background Scripts/findTableSize/Find Table Size.js @@ -0,0 +1,45 @@ +// Set the table you want to check +var tableName = 'sys_attachment_doc'; // You can change this to any table + +// Try to get the table size from sys_physical_table_stats +var tableStats = new GlideRecord('sys_physical_table_stats'); +tableStats.addQuery('table_name', tableName); +tableStats.setLimit(1); +tableStats.query(); + +var sizeGB = null; +var recordCount = null; + +if (tableStats.next()) { + sizeGB = tableStats.getValue('table_size_in_gb'); +} + +// If the table size is not available, estimate based on record count +if (!sizeGB) { + var grCount = new GlideAggregate(tableName); + grCount.addAggregate('COUNT'); + grCount.query(); + + if (grCount.next()) { + recordCount = parseInt(grCount.getAggregate('COUNT'), 10); + // Simple estimate: assume 1 KB per record + sizeGB = (recordCount / (1024 * 1024)).toFixed(2); + gs.info('Table [' + tableName + '] size not found in metadata. Estimated size: ' + sizeGB + ' GB'); + } else { + gs.info('Table [' + tableName + '] not found or contains no records.'); + } +} else { + gs.info('Table [' + tableName + '] size from metadata: ' + sizeGB + ' GB'); +} + +// Get record count properly +var grCountFinal = new GlideAggregate(tableName); +grCountFinal.addAggregate('COUNT'); +grCountFinal.query(); + +recordCount = 0; +if (grCountFinal.next()) { + recordCount = parseInt(grCountFinal.getAggregate('COUNT'), 10); +} + +gs.info('Table [' + tableName + '] contains ' + recordCount + ' records');