JessicaLanR
Kilo Guru

Hi there! Yes, I’ve worked on a REST API integration between ServiceNow and Cherwell, including the token endpoint. Your script is close, but there are a few things that might be causing issues in ServiceNow (even if it works fine in Postman).

Here are some tips to troubleshoot:

  1. Timeout: By default, RESTMessageV2 has a 30-second timeout. Make sure it’s not timing out.

  2. Error handling: Add a try/catch block to catch more specific errors from the ServiceNow instance.

  3. Authentication handling: Even if Postman works, ServiceNow might require a slightly different approach to headers or certificates — especially if you're using a MID Server.

Here’s a revised version of your script with some improvements:

try {
var endpoint = 'https://{domain}/CherwellAPI/token';
var clientId = '{client_id}';
var username = '{username}';
var password = '{password}';
var authMode = 'internal';

var request = new sn_ws.RESTMessageV2();
request.setEndpoint(endpoint);
request.setHttpMethod('POST');
request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

var requestBody = 'grant_type=password' +
'&username=' + encodeURIComponent(username) +
'&password=' + encodeURIComponent(password) +
'&client_id=' + encodeURIComponent(clientId) +
'&auth_mode=' + encodeURIComponent(authMode);

request.setRequestBody(requestBody);

var response = request.execute();
var responseBody = response.getBody();
var httpStatus = response.getStatusCode();

gs.info("Status: " + httpStatus);
gs.info("Response: " + responseBody);
} catch (ex) {
var message = ex.getMessage();
gs.error("Error calling Cherwell API: " + message);
}

 

Additional recommendations:

  • Check if there's a proxy between ServiceNow and the Cherwell domain.

  • Make sure the Cherwell SSL certificate is trusted by your instance (you may need to import it via System Definition > Certificates).

  • Enable RESTMessageV2 Debugging (System Logs > Outbound HTTP Requests) to get detailed logs of the request and response headers.

If the issue persists, feel free to share the exact HTTP status and error response so we can take a closer look.