How to use a REST API with RESTMessageV2 in Service Portal.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2017 10:53 AM
I've been working on implementing a 3rd party API for a customer. This is something i've never done before within Service Portal. Looking into the documentation (thank you) for ServiceNow
this task seemed like it would be relatively easy, but took a little while to figure out how to use it within Service Portal. Here is what I did:
1. Setup your REST API account with your Third Party. You will probably need an ID or Token for authentication if you're using Oauth.
2. Setup your REST Message by heading to System Web Services > Outbound > Rest Message and Create NEW. You should see a form that looks
something like the below image. You will need to create a name and an API endpoint to communicate with. You will also most likely need to put in http headers depending on the API's requirements.
3. Create HTTP Methods. In the below image you will see this notated by the Red Arrow. This can be the tricky part to understand but i'll explain. Since SN put everything into a table you can put your HTTP Verbs needed to interact with your external API into its own table and call those methods via the RESTMessageV2 API. THIS IS AWESOME!
4. Once you've created your HTTP methods you can now click on each method and test each call individually. Even better you will also see a link to 'Preview Script Usage'. This will show you the code needed to call your HTTP method via the RESTMessageV2 API. (see the second photo)
5. You will notice that the RESTMessageV2 API accepts two parameters and this is where i got confused via the documentation. The first param will be the name of your REST Message you created. In my case my REST Message name was 'Status Page'. The second param is not the HTTP Verb itself (GET,POST,PUT,PATCH,DELETE) but the name of the HTTP Verb Column your created. In my case it was 'Default GET'.
6. Finally I've placed my script within a Widget inside of Service Portal which logs out the Object. Now you will be able to interact with your Data!
var r = new sn_ws.RESTMessageV2('Status Page','Default GET');
var response = r.execute();
var responseBody = response.getBody();
console.log(responseBody);
- 11,682 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-05-2017 01:36 PM
This is awesome. Thanks Casey!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-06-2018 12:08 PM
Thanks, Casey for this article.
I have a doubt that in your step 6 of implementing the code, you have not set the parameters is that not required ?? and is that code written in clide script or Server Script.
And instead of using the widget, can we implement the same thing in UI page. Any thoughts please share.
Thanks,
Harsha.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-17-2020 01:00 PM
Wow this is perfect!
I did have a question about connecting to and consuming an external API that is behind a VPN? How would you go about this? I have hit a wall and don't know to get ServiceNow to reach my external API through the VPN.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-24-2024 05:41 AM
@Jace Nelson You could go to 'Filter Navigator'>IP Addresses' and you will see 'system security ip address control'. Here, you could add the ip address of the vpn you created.