logo

NJP

Scripted REST API in ServiceNow Inbound Integration solution with Custom Responses.

Import · Aug 05, 2022 · article

Introduction:

I have read lot of questions on community regarding the integrations for custom REST API.

May this article will helps you to gain knowledge on the scripted REST API and you can build some custom APIs on your own.

Using the scripted REST API feature you can build custom web service APIs.

Using this define service endpoints, query parameters, and headers for a scripted REST API, as well as scripts to manage the request and response.

Using this scripted REST API you can have full control over the data. You can add custom response messages as per business need.

Use case:

  1. In this article you will see the integration with the incident table take this as reference and implement with the required tables in ServiceNow.
  2. Here in the Script section checking the 3 scenarios.
  3. If it is a new request from the Source system(SAP, ServiceMax, AX, Sales force, D365 or other) then in Target system(ServiceNow) will create a new incident with the received data.
  4. If the incident already created then will update the data for existing incident in the ServiceNow.
  5. Last one if Source system sending invalid data then ignore and will not create in ServiceNow.

Steps:

1. Create the new scripted REST API using the new button as shown below.

image

2. Complete the form and provide the Name and API ID will populate accordingly. Save the form.

End Point or URL will extend the https://instancename.service-now.com + Base API path +Relative path

Base API path you can see below but Relative path will be available on Resource

Refer below one to create scripted API.

image

3. On related list on Resources create new Resource. Give name and select HTTP method as POST and save the form.

image

4. Once you save form will look like below and Relative Path is empty and you can see the Resource Path

Imp Note: Resource path = Base API path + Relative path

image

5. In below you can compare the Resource path after providing the Relative path.

Relative path will append to the Resource path.

image

6. In the script section we can push the request and response objects to the script include and we can implement the integration logic there but I am implementing it on script section itself to understand it easily.

7. Script: You can add fields as per your business requirement but here I am integrating few fields of incident table.

(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {

this.status = '200';

var respObj = {}; //declare the response object

var payload = request.body.data; //retrieving the JSON body

var number = payload.number; //getting the number from JSON

var callerid = payload.callerid;

var state = payload.state;

var category = payload.category;

var impact = payload.impact;

var urgency = payload.urgency;

var short_description = payload.short_description;

var description = payload.description;

var grinc = new GlideRecord("incident");

grinc.addQuery("number", number);

grinc.query();

if (grinc.next()) { // To update data on the existing incidents

grinc.caller_id = callerid;

grinc.state = state;

grinc.category = category;

grinc.impact = impact;

grinc.urgency = urgency;

grinc.short_description = short_description;

grinc.description = description;

grinc.update();

this.status = '200';

respObj.body = {

"message": "Updation Success!",

"detail": "Incident " + grinc.number + " updated successfully"

};

} else if (callerid) { //create incident if when callerid is not empty

grinc.initialize();

grinc.caller_id = callerid;

grinc.state = state;

grinc.category = category;

grinc.impact = impact;

grinc.urgency = urgency;

grinc.short_description = short_description;

grinc.description = description;

grinc.insert();

this.status = '200';

respObj.body = {

"message": "Creation Success!",

"detail": "Incident " + grinc.number + " created successfully"

};

} else { //ignore the creation if there is no caller info

this.status = '400';

respObj.body = {

"message": "Incident Ignored",

"detail": "Caller email is not received from Request"

};

}

if (this.status == '200') {

response.setBody(respObj);

response.setStatus(this.status);

return response;

} else {

var setError= new sn_ws_err.ServiceError(); //this API used to set the custom error messages

setError.setStatus(this.status);

setError.setMessage(respObj.body.message);

setError.setDetail(respObj.body.detail);

return setError;

}

})(request, response);

8. JSON Format: Below is the JSON format which will test using the postman.

"short_description": "Test short description",

"description": "Test description "

9. Once API creation is completed on Related List of Resource we have Explore REST API link click on that.

image

10. It will open the new tab and will look as below.

image

11. If you scroll down to Request Body on Raw section provide JSON data and refer below screen .

image

12. Create Incident: On above JSON not including the incident number so it will create the incident as per our script.

Once you click on send then will get the popup if you click on Ok. Then response section will look like below.

Below you can the status code and custom response with incident number.

image

Here providing the JSON with Incident number which created above.

Please see the updated JSON which is provided below.

image

image

image

On the JSON request if the callerid and number is empty then it will not create incident in ServiceNow

It will ignore the record and will get error response.

image

image

If it helps to you please Mark it as helpful and add comment if you have anything. Thanks!!

View original source

https://www.servicenow.com/community/itsm-articles/scripted-rest-api-in-servicenow-inbound-integration-solution/ta-p/2310389