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

How to write script rest API to fetch variable values in another instance

msc
Tera Contributor

Hi team,

How to write script rest API to fetch variable values in another instance.

Like we have dev and test instance.

I need to show the dev variable values in test instance. So how can I do this using scripted rest APIs.

1 REPLY 1

DUGGI
Giga Guru

@msc 

 

To fetch variable values from the dev instance and display them in the test instance using Scripted REST APIs, you need to create a Scripted REST API in the dev instance and then call that API from the test instance.

Step 1: Create a Scripted REST API in the dev instance

  1. In the dev instance, navigate to "System Web Services > Scripted REST APIs" and click "New".
  2. Provide a name and an API ID for your new Scripted REST API, then click "Submit".
  3. Click on "Resources" under the Related Links, then click "New".
  4. Provide the following details for the new resource:
    • HTTP Method: "GET"
    • Name: "Get Variable Values"
    • Script:
jsCopy code
(function process(/*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
  // Replace this with the actual table name and variable field names
  var tableName = 'your_table_name';
  var variable1Field = 'variable1_field_name';
  var variable2Field = 'variable2_field_name';

  var gr = new GlideRecord(tableName);
  gr.query();

  var result = [];
  while (gr.next()) {
    result.push({
      variable1: gr.getValue(variable1Field),
      variable2: gr.getValue(variable2Field)
    });
  }

  response.setContentType('application/json');
  response.setBody(JSON.stringify(result));
})();

  1. Click "Submit" to save the resource.

Step 2: Call the Scripted REST API from the test instance

Create a script in the test instance to call the Scripted REST API created in the dev instance. You can use this script in a Business Rule, a Scheduled Job, or any other place where you need to fetch the variable values.

jsCopy code
var devInstanceUrl = 'https://your_dev_instance.service-now.com';
var apiPath = '/api/your_api_id/get_variable_values'; // Replace 'your_api_id' with the actual API ID
var username = 'your_username';
var password = 'your_password';

var restMessage = new sn_ws.RESTMessageV2();
restMessage.setHttpMethod('GET');
restMessage.setEndpoint(devInstanceUrl + apiPath);
restMessage.setBasicAuth(username, password);

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

if (httpStatus === 200) {
  var variableValues = JSON.parse(responseBody);

  // Process the variable values as needed
  for (var i = 0; i < variableValues.length; i++) {
    var variable1Value = variableValues[i].variable1;
    var variable2Value = variableValues[i].variable2;

    // Do something with the variable values
  }
} else {
  gs.error('Error fetching variable values from the dev instance: ' + httpStatus);
}

Replace the placeholders in the scripts with your actual table and field names, as well as the authentication details and instance URL. This script will call the Scripted REST API from the dev instance and fetch the variable values, allowing you to process and display them in the test instance as needed.