The CreatorCon Call for Content is officially open! Get started here.

How to make a REST api call and process the data received in response?

Jamshaid
Tera Contributor

Hi Everyone

I am new to ServiceNow and trying to do the following stuff. I am not getting a lead to do it as I have been using traditional programming IDEs to solve different tasks but did not use an app like ServiceNow earlier. I am trying to do the following:

 

- make an outbound Rest call to an external RestAPI and get JSON back. (able to do this but the results can not be processed as using Outbound rest messages)

 - find a way to process the JSON response with javascript in the test app.

- make authentication with API token with the RestAPI call

Is there any way I could achieve such functionality? Thanks a lot

 

 

1 ACCEPTED SOLUTION

Check the "Advanced" checkbox.

find_real_file.png

The body of the script can be generated using REST Message.

  1. From the Navigator, go to "System Web Services" > " Outbound" > "REST Message".
  2. Create a new REST Message by clicking on the "New" button.
  3. Fill in the Endpoint (i.e. enter the URI of the external url. e.g. http://xxx.aaa.com:8080)
  4. Select "Authenentication type" from pull down
  5. If there is a HTTP Request header, select the "HTTP Request" tab and insert the required headers.
  6. Select the "New" button next to the HTTP Methods
  7. Enter the "Name" and the "HTTP method".
  8. To test the uri and if there is argument to be passed in the uri, click on the "New" button next to the "Variable Substitution". For example, to replace "id" with some value such as "abc"
  9. Click the "Test" link under "Related Links" to test the endpoint.
  10. If all goes well, click on the "Preview Script Usage" link under the "Related Links". This will generate JavaScript code template.
  11. Paste the generated source code to business rule.
  12. If using MID Server, edit the source code as needed.

To parse JSON response, 

      var responseBody = response.getBody();
      var response= JSON.parse(responseBody);
      for(i=0;i<response.length;i++) {
        // process json object
      }

 

View solution in original post

9 REPLIES 9

Community Alums
Not applicable

How we call restmsg into BR . In therotical word how we ans this. How we can call restmsg into BR?

Please advise.

Pavankumar_1
Mega Patron

Hi,

No those fields will not be available on any table. I took as example.

If you want to send the JSON then you need to create those output_payload(contains the json) and input payload (will get the response and captures here).

 

Example: If you need to send the a,b,c fields to x system. Then you can define the JSON and capture on field output_payload. so that we use that field on business rule to send data. 

If you need more experience on integrations then refer below video and other which are available on youtube.  

https://youtu.be/PC-h8U3Wuso

 

Hope it helps!!

Please Mark Correct/helpful, if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

thanks a lot for referring to that youtube channel. I got a lot to learn from there. The code sample you pasted did not help that much but I got a lot to learn. If you could post a formatted answer, I will mark it as answer.

Hitoshi Ozawa
Giga Sage
Giga Sage

Following ServiceNow tutorial explains very well on how to create an outbound REST message.

https://developer.servicenow.com/dev.do#!/learn/courses/rome/app_store_learnv2_rest_rome_rest_integr...

Following page has an concrete steps to create an outbound REST message to integrate with Google Calendar.

https://dxsherpa.com/blogs/create-a-rest-message-how-to-read-the-docs-integration-part-2/

Lastly, ServiceNow documentation. It will make more sense after going through the above 2 sites. These 3 sites should be sufficient for basic cases.

https://docs.servicenow.com/bundle/paris-application-development/page/integrate/outbound-rest/concep...

Thanks for sharing the resources. I will keep it in mind for my next tasks. Thanks a lot.