The CreatorCon Call for Content is officially open! Get started here.

Scripted REST API call

Arun_Manoj
Mega Sage

Hi Team,

 

I have the API endpoint , api key , user token and user.
How to get the data from a 3rd party tool to servicenow. using the above mention details. I want to fetch details from the api and print in logs to verify fields and values return.

 

please help @Ankur Agarwal 

 

Thanks

Arun Mnaoj

7 REPLIES 7

TippaluruK
Giga Contributor

Hello,

1. Create Scripted REST API
Go to System Web Services → Scripted REST APIs

Click New

Fill in the fields:

Name: Workday Integration API

API ID: workday_integration

Namespace: keep default or change as needed

Click Submit

2. Add a Resource (GET Method)
After saving the Scripted REST API:

Go to Resources → New

Fill in:

Field Value
Name Get Workday Data
HTTP Method GET
Relative Path get_data
Script Use the script below

Sample Script: Call Workday from Scripted REST API

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

var workdayUrl = 'https://api.workday.com/employee'; // your API endpoint

var r = new sn_ws.RESTMessageV2(); // Dynamic REST message
r.setEndpoint(workdayUrl);
r.setHttpMethod('GET');

// Set Headers
r.setRequestHeader("Accept", "application/json");
r.setRequestHeader("Content-Type", "application/json");
r.setRequestHeader("Authorization", "Bearer YOUR_USER_TOKEN");
r.setRequestHeader("API-Key", "YOUR_API_KEY"); // or use X-API-Key if required

try {
var res = r.execute();
var responseBody = res.getBody();
var httpStatus = res.getStatusCode();

// Log the response
gs.info('Workday API Status: ' + httpStatus);
gs.info('Workday API Response: ' + responseBody);

// Return response as JSON
response.setStatus(httpStatus);
response.setHeader("Content-Type", "application/json");
response.setBody(responseBody);

} catch (ex) {
gs.error('Error while calling Workday API: ' + ex.message);
response.setStatus(500);
response.setBody(JSON.stringify({ error: ex.message }));
}

})(request, response);

Test the API
Go to REST API Explorer

Choose your Scripted REST API → GET /workday_integration/get_data

Click Send

View the results and also check System Logs → All for gs.info() output.


Thanks,
Kishore.

Nishant8
Giga Sage
Giga Sage

Hello @Arun_Manoj, This can be achieved using outbound Rest Messages. Although, there might be some other materials available, I'll suggest you to go thru the product documentation available at https://developer.servicenow.com/dev.do#!/learn/learning-plans/yokohama/servicenow_application_devel....

Should you need any assistance during the integration by following the documentation, please feel free to post here, will be happy to assist you.

Your requirement seems to a basic one, but if you are going to integrate with external system in production, then please try to explore their end points, payloads structure etc, and then try with postman first and then implement here in ServiceNow.

 

Regards,

Nishant

Subramanyam V
Giga Expert

Hi,

 

If you are looking for workday connectivity from ServiceNow ; then you need to do some configuration steps like following :

 

  • Register an OAuth provider in ServiceNow for Workday. 
    • Go to System OAuth > Application Registry.
    • Click New > Connect to a third party OAuth Provider.
      • Name: Workday OAuth
      • Client ID: (from Workday)
      • Client Secret: (from Workday)
      • Token URL: (from Workday, e.g., https://workday.example.com/oauth2/token)
      • Authorization URL: (from Workday)
      • Redirect URL: (ServiceNow instance URL, e.g., https://<instance>.service-now.com/oauth_redirect.do)
  • Configure the REST Message to use OAuth and Script the REST API to use the OAuth profile for authentication
    • script similar to below 

    •     var r = new sn_ws.RESTMessageV2();
          r.setHttpMethod('GET');
          r.setEndpoint(workdayUrl);

          // Set OAuth profile (replace with your profile name)
          r.setAuthenticationProfile('oauth2', 'Workday OAuth');

          // Optional: Set headers if needed
          // r.setRequestHeader('Accept', 'application/json');

          var res = r.execute();
          var responseBody = res.getBody();
          var httpStatus = res.getStatusCode();

          gs.info('Workday API Response: ' + responseBody);

          response.setStatus(httpStatus);
          response.setBody(responseBody);
  • Test - 
    • Publish your Scripted REST API.
    • Call it (e.g., via Postman or ServiceNow REST API Explorer or Python or Insomnia ).
    • Check logs for the Workday response.

 

Regards

Subbu