- Subscribe to RSS Feed
- Mark as New
- Mark as Read
- Bookmark
- Subscribe
- Printer Friendly Page
- Report Inappropriate Content
In my last blog I provided the Example of POST method to update the values, I hope it provided the basic idea of Scripted REST APIs. In this I am sharing an example of GET method i.e to retrieve data from Service Now tables using Scripted REST APIs.
Here we will script to retrieve the assignment group field value by passing the change number in URL. Initial steps to create Scripted REST APIs and Resource would be same as shown in the last Blog.
- For this I have added the change_number as parameter in the 'Relative path' field in the below format.
- So when change number is passed in the URL, it can be obtained in the script using pathParams of request object. setContentType method is used to set the content type of the response being sent to the client.
- To test this, create a new REST message, provide the name and the Endpoint.
- Then open the GET method from the related list and configure as below.
- Test the connection. You can see the response in the 'Response' field.
- Hit the URL , you can see get the response in JSON format.
- For getting the response in XML, we need to convert the object to XML using XMLhelper class (Check script include XMLhelper —it converts XML to and from JavaScript objects) and then write the string data to the response stream using RESTAPIResponseStream object. Before using RESTAPIResponseStream object we need to set the content type of the response to 'application/xml' .
- Now test the code.
- Open the URL.
Also, we can get an output by just using return body, try that by replacing the code after if condition.
Attached is the code used in this blog.
If you find the blog useful, please like/bookmark/share (on the top right corner) .
Reference Links:
Note: This requirement can be done using simple REST API and other methods as well. I have just presented another way of doing it using Scripted REST APIs.
- 13,830 Views
- « Previous
-
- 1
- 2
- Next »
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.