How to write script rest API to fetch variable values in another instance
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2023 09:59 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-26-2023 02:45 PM
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
- In the dev instance, navigate to "System Web Services > Scripted REST APIs" and click "New".
- Provide a name and an API ID for your new Scripted REST API, then click "Submit".
- Click on "Resources" under the Related Links, then click "New".
- 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)); })();
- 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.