Deepa Srivastav
Kilo Sage

This module/feature was newly introduced from Geneva release. I did a small POC to learn about it which I am going to share with you all. It has all the detailed steps for those who want to get hands on this.

Use case :   We want to update few fields in our service now table with the values from some trusted source and validate them if they are in correct format before updating
them to the record.

Step 1: Go to 'Scripted REST APIs' module and create a new one. The API ID will be automatically populated based on the Name. Save the form. Make note of 'Base API path', we will be using it later.

find_real_file.png

Step 2: Next we have to create a new Resource (see related list). Provide a name, select the HTTP method this resource implements. For our case as we have to update the data,   so used
POST Method. Save the resource.


find_real_file.png

----------------------------------------------------
find_real_file.png

Step 3: To fetch the values from the source and validate it we have to write script in the 'Script' part in the resource. Request.body.data (RESTAPIRequestBody) object allows you to access the body content of a scripted REST API request in scripts. The highlighted part gives the response whether update was successful or not.


find_real_file.png

Step 4: To test this I created a REST message. Create a new REST message, provide the name , endpoint as instanceurl/Base API path (this base API path we got in step1). Save the
form.

Step 5: HTTP methods (GET,POST,PUT,PATCH)   will appear at the bottom. Open the POST and configure as below.
find_real_file.png


find_real_file.png

Step 6: Goto the HTTP Request tab and Enter the data to be tested in the 'Content' field. Various request and response format are supported for this like JSON,XML,test/XML.   I used JSON format for request and response.   We need some unique field (as INC number) so as to identify the record to be updated.

find_real_file.png

Step7: Now time to Test the connection (click test as shown in above screenshot).


find_real_file.png

You can see the HTTP status as 200 which means that connection was successful. But as I provided the year in wrong format I got the customized response which I gave in the script.

We may get different HTTP status if there is some or the other connection errors. Check below link for more details.

https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/integrate/inbound-rest/referenc...

https://docs.servicenow.com/bundle/helsinki-servicenow-platform/page/integrate/custom-web-services/c...

If you want resource to require authentication and not be public, check 'Require Authentication' in the resource. Then in the REST message in the 'Authentication' Tab select the 'Authentication type' as basic. Create a new Basic Auth Profile with some Name, username and password of a user from User(sys_user) table.

find_real_file.png

I hope this blog will help you to give an idea about Scripted REST APIs. There are still many areas in this which needs to be explored. I will write once I have them explored.

Will be coming up with GET example on this soon...

Developer Community

Please encourage me to write more by hitting on like/share/bookmark (on the top right corner)

10 Comments
Mohammed Lais1
Mega Guru

Hi Deepa,


Thanks for writing this blog. This article is very helpful.


I believe we can do the same process using scripted SOAP services also.


will you please provide the difference between Scripted SOAP and Scripted REST service and when   to use which service.


Deepa Srivastav
Kilo Sage

Thanks for your kind words Mohammed, yes we can do this using SOAP too. Please check below for difference between them.


SOAP vs REST web services - javatpoint


http://blog.smartbear.com/apis/understanding-soap-and-rest-basics/


Deepa Srivastav
Kilo Sage
vamsi
Tera Contributor

Hi Deepa,



Nice article neatly explained, informative as well as very easy to understand keep posting more .



Thanks,


Vamsi


Deepa Srivastav
Kilo Sage

Thanks so much Vamsi for your valuable comment


BenPhillipsSNC
Kilo Guru

Thank you! It was helpful.


now_dev
Mega Expert

Hi Deepa,



Thanks for writing this blog. It was helpful and easy to understand.



I am trying to create a REST service where REST HTTP headers values are dynamically generating. Can you pls suggest if scripted web services can be used for such type of request?


BenPhillipsSNC
Kilo Guru

Hi Deepa,



I have a question. Are Scripted REST Resources multithreaded? (concurrency)



I am trying to understand how they work... If a user is accessing it, it is taking up a REST semaphore right? Could there be multiple REST Resources used by that user at the same time? If so would that one user take up several semaphores concurrently?



Thanks


yogesh15dd
Tera Guru

Hi Deepa,



We are trying to set the 'due_date' field for the 'request' record (sc_request) using a the rest api, it will not set it some how, i have set up the time zone for the user record (we used to submit the api request), even then..



What do you think the issues is?



Thanks


Yogish


Shital Revandka
Giga Expert

Hello,


I need to create a Webservice API where input is: Sys ID of any record and output is: all *related* active records to that sys id. The relationships between the tables is maintained in Relationships under system definition.


Could you please guide me how to implement this? Sample code would be great.


Input: Sys ID of the record
Output: All *RELATED* active records from *VARIOUS* tables (in the following JSON format):
{
"result": [
{
"Sys ID": "5520267",
"CI Name": "Record 1",
"Table Name": "u_table_a"
},
{
"Sys ID": "5520367",
"CI Name": "Record 2",
"Table Name": "u_table_a"
},
{
"Sys ID": "8331210",
"CI Name": "Record 1",
"Table Name": "u_table_b"
},
{
"Sys ID": "8321210",
"CI Name": "Record 2",
"Table Name": "u_table_b"
},
{
"Sys ID": "3042006",
"CI Name": "Record 3",
"Table Name": "u_table_b"
},
{
"Sys ID": "4509847",
"CI Name": "Record 1",
"Table Name": ""u_table_c"
}
{
"Sys ID": "4509247",
"CI Name": "Record 2",
"Table Name": ""u_table_c"
}
]
}