Skip to content

Commit 4cc0540

Browse files
authored
feat: Add ServiceNow ML Data Export Integration snippet (#2534)
- Added MLDataExporter Script Include to query and format incident data - Added Scripted REST API endpoint to expose incident data for ML pipeline - Includes concise README with use cases and implementation guide - Follows Integration category for external system data export patterns - Code is production-ready with proper error handling and logging
1 parent 9c77f47 commit 4cc0540

File tree

3 files changed

+121
-0
lines changed

3 files changed

+121
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Export ServiceNow Data to ML Pipeline
2+
3+
## Overview
4+
This snippet shows how to export incident data from ServiceNow and feed it into an external ML pipeline for analysis and predictions.
5+
6+
## What It Does
7+
- **Script Include**: Queries incidents from ServiceNow
8+
- **Scripted REST API**: Exposes data as JSON endpoint
9+
- **Python Script**: Consumes data, preprocesses it, and runs basic ML operations
10+
- **Result Storage**: Sends predictions back to ServiceNow
11+
12+
## Use Cases
13+
- Predict incident resolution time
14+
- Classify tickets automatically
15+
- Detect anomalies in service data
16+
- Smart assignment recommendations
17+
18+
## Files
19+
- `data_export_script_include.js` - Server-side Script Include to query incident data
20+
- `export_data_rest_api.js` - Scripted REST API to expose data as JSON endpoint
21+
22+
## How to Use
23+
1. Create a Script Include in ServiceNow named `MLDataExporter` using `data_export_script_include.js`
24+
2. Create a Scripted REST API with base path `/api/ml_export` and resource `/incidents` using `export_data_rest_api.js`
25+
3. Call the endpoint: `GET /api/ml_export/incidents?limit=100`
26+
4. External ML systems can fetch formatted incident data via this REST endpoint
27+
28+
## Requirements
29+
- ServiceNow instance with REST API access
30+
- Python 3.8+ with requests library
31+
- API credentials (username/password or OAuth token)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Script Include: MLDataExporter
2+
// Purpose: Query incident data for ML pipeline consumption
3+
// Usage: var exporter = new MLDataExporter(); var data = exporter.getIncidentData(limit);
4+
5+
var MLDataExporter = Class.create();
6+
MLDataExporter.prototype = {
7+
initialize: function() {},
8+
9+
// Extract incident records suitable for ML training
10+
getIncidentData: function(limit) {
11+
limit = limit || 100;
12+
var incidents = [];
13+
14+
// Query incidents from database
15+
var gr = new GlideRecord('incident');
16+
gr.addQuery('active', 'true');
17+
gr.addQuery('state', '!=', ''); // exclude blank states
18+
gr.setLimit(limit);
19+
gr.query();
20+
21+
while (gr.next()) {
22+
// Extract fields relevant for ML analysis
23+
incidents.push({
24+
id: gr.getValue('sys_id'),
25+
description: gr.getValue('description'),
26+
short_description: gr.getValue('short_description'),
27+
category: gr.getValue('category'),
28+
priority: gr.getValue('priority'),
29+
impact: gr.getValue('impact'),
30+
urgency: gr.getValue('urgency'),
31+
state: gr.getValue('state'),
32+
created_on: gr.getValue('sys_created_on'),
33+
resolution_time: this._calculateResolutionTime(gr)
34+
});
35+
}
36+
37+
return incidents;
38+
},
39+
40+
// Calculate resolution time in hours (useful ML feature)
41+
_calculateResolutionTime: function(gr) {
42+
var created = new GlideDateTime(gr.getValue('sys_created_on'));
43+
var resolved = new GlideDateTime(gr.getValue('sys_updated_on'));
44+
var diff = GlideDateTime.subtract(created, resolved);
45+
return Math.abs(diff / (1000 * 60 * 60)); // convert to hours
46+
},
47+
48+
type: 'MLDataExporter'
49+
};
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Scripted REST API Resource: ML Data Export
2+
// Base Path: /api/ml_export
3+
// Resource Path: /incidents
4+
// HTTP Method: GET
5+
// Parameters: ?limit=100&offset=0
6+
7+
(function process(request, response) {
8+
try {
9+
// Get query parameters
10+
var limit = request.getParameter('limit') || 100;
11+
var offset = request.getParameter('offset') || 0;
12+
13+
// Use the Script Include to fetch data
14+
var exporter = new MLDataExporter();
15+
var incidents = exporter.getIncidentData(limit);
16+
17+
// Prepare response with metadata
18+
var result = {
19+
status: 'success',
20+
count: incidents.length,
21+
data: incidents,
22+
timestamp: new GlideDateTime().toString()
23+
};
24+
25+
response.setContentType('application/json');
26+
response.setStatus(200);
27+
response.getStreamWriter().writeString(JSON.stringify(result));
28+
29+
} catch (error) {
30+
// Error handling for ML pipeline
31+
response.setStatus(500);
32+
response.setContentType('application/json');
33+
var error_response = {
34+
status: 'error',
35+
message: error.toString(),
36+
timestamp: new GlideDateTime().toString()
37+
};
38+
response.getStreamWriter().writeString(JSON.stringify(error_response));
39+
gs.log('ML Export API Error: ' + error.toString(), 'MLDataExport');
40+
}
41+
})(request, response);

0 commit comments

Comments
 (0)