Export excel data from ServiceNow to the external servers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:29 AM
Hi,
I have a requirement like
I need to export the data from ServiceNow to the external servers.
For this I created an export set and export the data to the Mid server. But from the Mid server to the external server(Out side of ServiceNow) how to export the data.
Can some one please help me on this.
Thanks in Advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 07:13 AM
Does this address the requirement?
best way to have a request available to external u... - ServiceNow Community
Hope this helps,
John
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 11:08 AM
Hi @John235 ,
No, My requirement was I need to send the incident export to the external servers from Snow.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 08:52 PM
Hi Ashok,
1. Determine the Export Format
Decide the format for the export (e.g., JSON, XML, CSV) based on what the external server expects.
2. Create a Script to Export Data
You can use a script in ServiceNow to gather and format the incident data. This script could be triggered manually, by a scheduled job, or by a specific event.
Example: Exporting Data as JSON
Create a Script Include:
Navigate to System Definition > Script Includes.
Click New to create a new Script Include.
Set Client Callable to false if the script is only for server-side use.
Script Include Code:
var IncidentExportUtil = Class.create();
IncidentExportUtil.prototype = {
initialize: function() {},
exportIncident: function(incidentSysId) {
var gr = new GlideRecord('incident');
if (gr.get(incidentSysId)) {
var incidentData = {
number: gr.getValue('number'),
short_description: gr.getValue('short_description'),
description: gr.getValue('description'),
state: gr.getValue('state')
};
return JSON.stringify(incidentData);
}
return null;
}
};
Create a Scripted REST API:
Go to System Web Services > Scripted REST APIs.
Click New to create a new REST API.
Configure the API:
Name: Provide a name for the API.
API ID: Set a unique ID.
API Documentation: Optionally, you can add documentation here.
Create a Resource:
Click on the Resources tab.
Click New to create a new resource.
HTTP Method: Set to GET or POST based on your needs.
Relative Path: Define a path, e.g., /export/{incident_sys_id}.
Script for the Resource:
(function(request, response) {
var incidentSysId = request.pathParams.incident_sys_id;
var exportUtil = new IncidentExportUtil();
var incidentData = exportUtil.exportIncident(incidentSysId);
if (incidentData) {
response.setContentType('application/json');
response.setBody(incidentData);
} else {
response.setStatus(404);
response.setBody('Incident not found.');
}
})(request, response);
3. Send Data to External Server
Create a Scripted REST API for Sending Data:
Create another Scripted REST API if you want to handle sending data from ServiceNow to the external server.
Scripted REST API Code:
var ExternalDataSender = Class.create();
ExternalDataSender.prototype = {
initialize: function() {},
sendIncidentData: function(incidentData) {
var requestBody = JSON.stringify(incidentData);
var request = new GlideHTTPRequest('https://external.server/api/endpoint'); // Replace with actual endpoint
request.setMethod('POST');
request.setRequestHeader('Content-Type', 'application/json');
request.setRequestBody(requestBody);
var response = request.execute();
return response.getStatusCode();
}
};
Schedule the Export Process:
Go to System Definition > Scheduled Jobs.
Click New to create a new scheduled job.
Run: Choose how frequently you want the export to happen.
Script:
(function executeScheduledJob() {
var gr = new GlideRecord('incident');
gr.query(); // Add conditions if needed
var sender = new ExternalDataSender();
while (gr.next()) {
var incidentData = {
number: gr.getValue('number'),
short_description: gr.getValue('short_description'),
description: gr.getValue('description'),
state: gr.getValue('state')
};
var statusCode = sender.sendIncidentData(incidentData);
gs.info('Sent incident ' + gr.getValue('number') + ' with status code ' + statusCode);
}
})();
4. Testing and Validation
Test your Scripted REST API: Use the REST API Explorer or tools like Postman to test your endpoints.
Check external server: Ensure that data is correctly received and processed by the external server.
Thanks!