Skip to content

Commit 24942ab

Browse files
Sync Data Between Two ServiceNow Instances via REST API (#1381)
* Create SyncData.js The script leverages the ServiceNow REST API to retrieve records from a specified table on the source instance, then transmits them to the target instance for insertion. As an example, it is set up to sync active user records, but it can be easily modified for any other table and filter criteria. Usage: The script is configured with the following parameters: table: Specifies the name of the table to sync (Example is sys_user). query: A GlideRecord query string to filter the records to be synchronized targetInstance: The ServiceNow instance to which data will be sent. user and password: Credentials for authenticating with the target instance. * Create readme.md The script leverages the ServiceNow REST API to retrieve records from a specified table on the source instance, then transmits them to the target instance for insertion. As an example, it is set up to sync active user records, but it can be easily modified for any other table and filter criteria. Usage: The script is configured with the following parameters: table: Specifies the name of the table to sync (Example is sys_user). query: A GlideRecord query string to filter the records to be synchronized targetInstance: The ServiceNow instance to which data will be sent. user and password: Credentials for authenticating with the target instance.
1 parent 9ad7cb4 commit 24942ab

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//Add below Details
2+
var table = "sys_user";
3+
var query = "active=true";
4+
var targetInstance = "dev1234";
5+
var user, password;
6+
7+
sendData(table, targetInstance, query, user, password);
8+
9+
function sendData(table, targetInstance, query) {
10+
var pload = {};
11+
var tableGR = new GlideRecord(table);
12+
if (query) {
13+
tableGR.addQuery(query);
14+
}
15+
tableGR.query();
16+
while (tableGR.next()) {
17+
var dictionaryGR = new GlideRecord('sys_dictionary');
18+
dictionaryGR.addQuery('name=' + table);
19+
dictionaryGR.query();
20+
while (dictionaryGR.next()) {
21+
var element = dictionaryGR.element.toString();
22+
pload[dictionaryGR.element] = tableGR.getValue(element);
23+
}
24+
var requestBody = JSON.stringify(pload);
25+
//gs.info(requestBody);
26+
var request = new sn_ws.RESTMessageV2();
27+
request.setEndpoint('https://' + targetInstance + '.service-now.com/api/now/table/' + table);
28+
request.setHttpMethod('POST');
29+
request.setBasicAuth(user, password);
30+
request.setRequestHeader("Accept", "application/json");
31+
request.setRequestBody(requestBody);
32+
var response = request.execute();
33+
gs.log(response.getBody());
34+
}
35+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
The script leverages the ServiceNow REST API to retrieve records from a specified table on the source instance, then transmits them to the target instance for insertion.
2+
3+
As an example, it is set up to sync active user records, but it can be easily modified for any other table and filter criteria.
4+
5+
6+
Usage:
7+
8+
The script is configured with the following parameters:
9+
10+
table: Specifies the name of the table to sync (Example is sys_user).
11+
12+
query: A GlideRecord query string to filter the records to be synchronized
13+
14+
targetInstance: The ServiceNow instance to which data will be sent.
15+
16+
user and password: Credentials for authenticating with the target instance.

0 commit comments

Comments
 (0)