Working with External Data · Topic 2 of 2

Integrating with REST

REST integration for the ServiceNow CAD exam: REST Messages, RESTMessageV2, the REST API Explorer, Scripted REST APIs, status codes and outbound logs.

  • 5 min
  • Includes a diagram
In a nutshell
  • Outbound: REST Message plus HTTP Method records, called with sn_ws.RESTMessageV2.
  • ${name} variables are filled with setStringParameterNoEscape() or setStringParameter().
  • The Test link checks an HTTP Method; the REST API Explorer checks inbound APIs.

What it is

REST lets your application exchange data with another system over HTTP, usually as JSON. ServiceNow works in both directions. Outbound, the instance calls an external API, for example to fetch a weather forecast. Inbound, an external system calls the instance, through the Table API or a Scripted REST API that you build.

For the exam you need to know which record to create, how to call it from script, how to test it and where to look when it fails.

Business rule or scriptRESTMessageV2REST Messageendpoint, auth, HTTP methodExternal APIResponsestatus code, body

How it works

Outbound: REST Message records

An outbound call is defined as a REST Message under System Web Services > Outbound > REST Message. The REST Message holds the base endpoint and the authentication, such as a basic auth profile or an OAuth 2.0 profile. Each HTTP Method record below it (GET, POST, PUT, PATCH, DELETE) holds its own endpoint, headers, query parameters and request body.

Endpoints and bodies can contain variables written as ${name}, which a script fills at run time. The Test related link on an HTTP Method sends the request and shows the status code and response body. Preview Script Usage shows ready-made RESTMessageV2 code for that method.

For an endpoint inside a private network, the request can be sent through a MID Server instead of straight from the instance.

Outbound: calling it from script

Outbound REST: a server script creates sn_ws.RESTMessageV2 from a REST Message record and HTTP Method, calls execute to reach the remote API directly or through a MID Server for private network endpoints, and reads the RESTResponseV2 status code and body.

var r = new sn_ws.RESTMessageV2('Weather API', 'Get forecast');
r.setStringParameterNoEscape('city', current.getValue('u_city'));
var response = r.execute();
if (response.getStatusCode() == 200) {
  var data = JSON.parse(response.getBody());
  current.u_forecast = data.summary;
}

execute() waits for the answer. executeAsync() returns at once, which suits slow APIs, and waitForResponse() can be called later if the answer is needed. Avoid long synchronous calls in a before business rule, where the user waits for the save.

Inbound: Table API and REST API Explorer

The Table API exposes records at /api/now/table/<table name> with GET, POST, PUT, PATCH and DELETE. Every call runs as the authenticated user, so normal ACLs decide what that user may read or change.

The REST API Explorer (System Web Services > REST > REST API Explorer) builds and sends test requests to the instance's own APIs and shows the request URL, headers, response and sample code. It needs the rest_api_explorer, web_service_admin or admin role. It tests inbound APIs; it does not test outbound REST Messages.

Inbound: Scripted REST APIs

When the Table API does not fit, for example you want one call to return data from several tables, build a Scripted REST API under System Web Services > Scripted REST APIs. Its base path is built from the application namespace and the API ID. Each resource has an HTTP method, a relative path such as /order/{id} and a script.

(function process(request, response) {
  var gr = new GlideRecord('x_acme_orders_order');
  if (!gr.get(request.pathParams.id)) {
    response.setStatus(404);
    return;
  }
  response.setBody({ number: gr.getValue('number'), state: gr.getValue('state') });
})(request, response);

request.pathParams, request.queryParams and request.body.data carry the input. Resources require authentication by default and can be protected by REST ACLs.

Status codes to recognize

CodeMeaning in a ServiceNow scenario
200Success, data returned
201Record created by a POST
204Success with no body, such as a Table API DELETE
400Bad request, such as malformed JSON
401Not authenticated: missing or wrong credentials
403Authenticated, but an ACL denies the action
404Resource or record not found, such as a DELETE repeated on a deleted record

Testing and debugging

For outbound calls, open System Logs > Outbound HTTP Requests to see each request, endpoint, status and timing; the HTTP log level on the REST Message or HTTP Method controls how much detail is kept. In script, log getStatusCode(), getBody() and getErrorMessage(). For inbound calls, reproduce the request in the REST API Explorer, then check ACLs and the system log.

How the exam asks about this

  • "Store the endpoint and credentials once and call them from several scripts." Create a REST Message with HTTP Methods and call it with RESTMessageV2.
  • "An integration user gets 403 calling a Scripted REST resource." The user is authenticated but fails the ACL; grant the needed role.
  • "Return combined data from two tables in a single call." Build a Scripted REST API resource.

What to remember for the exam

  • Outbound: REST Message plus HTTP Method records, called with sn_ws.RESTMessageV2.
  • ${name} variables are filled with setStringParameterNoEscape() or setStringParameter().
  • The Test link checks an HTTP Method; the REST API Explorer checks inbound APIs.
  • Table API path is /api/now/table/<table name> and respects ACLs.
  • 401 means not authenticated, 403 means an ACL denied, 404 means not found.
  • Outbound calls are logged under System Logs > Outbound HTTP Requests.
Watch out

A Scripted REST API runs as the calling user. Returning 200 in testing as admin proves nothing about the integration user; test with that account's roles.

Common mistakes

Hard coding endpoints and passwords in scripts. Put them in a REST Message and a credential record, so one change updates every caller.

Reading 403 as a credentials problem. Wrong credentials give 401. A 403 means the user is known but an ACL blocks the action.

Using the REST API Explorer to debug an outbound call. It only calls the instance's own APIs. For outbound problems, use the HTTP Method Test link and the outbound HTTP log.

Check yourself

  • Which API calls a REST Message from a server script?

    sn_ws.RESTMessageV2, created with the REST Message name and the HTTP Method name, then execute() or executeAsync().

  • An inbound call returns 401. What is the likely cause?

    The request is not authenticated, for example missing or wrong credentials; an ACL failure would return 403.

  • Where do you check what an outbound REST call actually sent and received?

    In System Logs > Outbound HTTP Requests, with the HTTP log level set high enough to keep the detail you need.

Topic 2 of 2 in Working with External Data

Test yourself on Working with External Data

The full ServiceNow CAD question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by ServiceNow. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.