How to get ci records from Mongo DB database to servicenow using rest api.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 07:24 AM
I need to get all the data from the Mongo DB database through the rest API in ServiceNow.
*How should I proceed with this requirement?
* I am new to rest API integration, Can anyone explain the process?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-20-2023 07:31 AM
Hi @Kishor O
1) You will need the MongoDB connection details, including the host, port, and authentication credentials, to connect to the database.
2) In ServiceNow, you can create a scripted REST API endpoint to interact with the MongoDB database. Choose an authentication type (typically "Basic" if MongoDB is secured). Here's an example script to fetch data from MongoDB using the Node.js MongoDB driver:
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var MongoClient = require('mongodb').MongoClient;
var url = 'mongodb://username:password@mongo-host:port/database';
MongoClient.connect(url, function(err, client) {
if (err) {
response.setStatus(500);
response.setBody({ error: err.toString() });
response.setContentType('application/json');
response.setError(new Error(err));
return;
}
var db = client.db('your-database-name');
var collection = db.collection('your-collection-name');
collection.find({}).toArray(function(err, documents) {
if (err) {
response.setStatus(500);
response.setBody({ error: err.toString() });
response.setContentType('application/json');
response.setError(new Error(err));
client.close();
return;
}
response.setStatus(200);
response.setBody(documents);
response.setContentType('application/json');
client.close();
});
});
})(request, response);
Replace 'username', 'password', 'mongo-host', 'port', 'database', and 'your-collection-name' with your MongoDB credentials and database information.
3) In the "Access" tab of your Scripted REST API, configure access permissions based on your requirements. For example, you can specify which roles or users are allowed to access the API.
4) After saving the scripted REST API, you can test it by making a GET request to the API's URL. The URL for your custom endpoint will be something like:
https://instance.service-now.com/api/your-api-id/your-endpoint
Replace instance with your ServiceNow instance name, your-api-id with the API ID you configured, and your-endpoint with the name you gave to your endpoint.
This request should return data from your MongoDB database in JSON format.
You may also try "REST API Explorer" related link to test the same.
Hope this info helps you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 12:30 PM
I tried the same but getting below error. Were you able to get the data collection from MongoDB?
{
"error": { "message": "\"require\" is not defined.",
"detail": "ReferenceError: \"require\" is not defined. (sys_ws_operation.adc25ffd87bf39100c6d40c7cebb3598.operation_script; line 2)"
},
"status": "failure"
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2024 10:13 PM
I have done this but followed different approach . Can you share what endpoint are you using to get data?