- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-06-2022 11:18 PM
Hi All,
I would like to have an integration to pull the data from an external source and create records in a custom table. The daa will be pulled every sec and should create/update the corresponding records. Can someone help? The external API is created with a token for security purpose.
The API Information sample is below.
GET https://mydomain.net/api/vm/
Content-Type: application/json
Authorization: token <insert token here>
Also below is the JSON Sample.
[
{
"vm": "vm-123",
"name": "ABCSharePoint",
"power_state": "POWERED_ON",
"cpu_count": 4,
"memory_size_MiB": 24576,
"resource_pool": "resgroup-123",
"pool_name": "ABC-SQL",
"vsphere_instance": "ABC_INFRA",
"total_disk_MiB": 697796,
"timestamp": "2022-01-03T12:23:20.781822+04:00"
},
{
"vm": "vm-12345",
"name": "XYZ-ADC1",
"power_state": "POWERED_OFF",
"cpu_count": 4,
"memory_size_MiB": 16384,
"resource_pool": "resgroup-1234",
"pool_name": "XYZ-DC",
"vsphere_instance": "ABC_INFRA",
"total_disk_MiB": 42949,
"timestamp": "2022-01-03T12:23:20.981633+04:00"
}
]
Thanks,
Rijeesh Rathnakumar
Solved! Go to Solution.
- Labels:
-
Integrations
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-17-2022 09:38 AM
Hi,
Please find the steps below to achieve your requirement:
1) Create a Outbound Rest API by navigating to the module "Outbound Rest" as shown below:
2) Click on New button and give a valid Name as shown below:
3) Now click on New under HTTP Method as shown below and select method as "Get"
4) Define your HTTP header and then you can write a Async or After Insert and Update Business Rule on your table where you want and use the script below to fetch the response
Sample script shared below to be used in BR:
try {
var r = new sn_ws.RESTMessageV2('Get Records', 'Get Records');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');
//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute();
var responseBody = response.getBody();
for(var i=0;i<responseBody.length;i++){
createRecord(responseBody[i]);
}
var httpStatus = response.getStatusCode();
function createRecord(responseBody[i]){
var gr = new GlideRecord('Table Name');
gr.initialize();
gr.FIeldName = responseBody[i].vm;
gr.FIeldName = responseBody[i].name;
gr.FIeldName = responseBody[i].power_state;
gr.FIeldName = responseBody[i].cpu_count;
gr.insert();
}
}
catch(ex) {
var message = ex.message;
}
This way you can fetch the response and then parse it and insert record.. Let me know if you are facing an issue.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-16-2022 10:32 PM
Hi,
You can use the below thread for configuring the BR to invoke the REST API.
https://community.servicenow.com/community?id=community_question&sys_id=bfd5e5ecdb1c23048e7c2926ca961934
https://community.servicenow.com/community?id=community_question&sys_id=d357b366db3cdb402b6dfb651f961917
Article : https://community.servicenow.com/community?id=community_article&sys_id=e3a106b7db7c1854414eeeb5ca961998
Once you get the Response Body then you can create or update the records in your related table based on your requirement and validations.
Mark this as Helpful/Correct in case this helps you in anyways.
Regards,
Sourabh
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-17-2022 09:38 AM
Hi,
Please find the steps below to achieve your requirement:
1) Create a Outbound Rest API by navigating to the module "Outbound Rest" as shown below:
2) Click on New button and give a valid Name as shown below:
3) Now click on New under HTTP Method as shown below and select method as "Get"
4) Define your HTTP header and then you can write a Async or After Insert and Update Business Rule on your table where you want and use the script below to fetch the response
Sample script shared below to be used in BR:
try {
var r = new sn_ws.RESTMessageV2('Get Records', 'Get Records');
//override authentication profile
//authentication type ='basic'/ 'oauth2'
//r.setAuthenticationProfile(authentication type, profile name);
//set a MID server name if one wants to run the message on MID
//r.setMIDServer('MY_MID_SERVER');
//if the message is configured to communicate through ECC queue, either
//by setting a MID server or calling executeAsync, one needs to set skip_sensor
//to true. Otherwise, one may get an intermittent error that the response body is null
//r.setEccParameter('skip_sensor', true);
var response = r.execute();
var responseBody = response.getBody();
for(var i=0;i<responseBody.length;i++){
createRecord(responseBody[i]);
}
var httpStatus = response.getStatusCode();
function createRecord(responseBody[i]){
var gr = new GlideRecord('Table Name');
gr.initialize();
gr.FIeldName = responseBody[i].vm;
gr.FIeldName = responseBody[i].name;
gr.FIeldName = responseBody[i].power_state;
gr.FIeldName = responseBody[i].cpu_count;
gr.insert();
}
}
catch(ex) {
var message = ex.message;
}
This way you can fetch the response and then parse it and insert record.. Let me know if you are facing an issue.
Hope this helps. Please mark the answer as correct/helpful based on impact.
Regards,
Shloke
Regards,
Shloke
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-17-2022 11:36 PM
Hello Shloke,
Thank you very much for the detailed steps. I am getting some error (line 27) while copying your script. Can you please guide me?
Also, I have a small doubt.. If I create a business rule on the same table (target table), how do i get the data from the source (REST) automatically? Can we schedule a refresh or do some scheduled import? Please advise.
Thanks,
Rijeesh Rathnakumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-20-2022 05:15 AM
Hello Everyone,
The below Youtube Video is really great in explaining how we can configure the REST Message and schedule it.
https://www.youtube.com/watch?v=qJ9wphm41iY
Thank you all for your great help.
Thanks,
Rijeesh Rathnakumar