logo

NJP

GlideRemoteGlideRecord API Magic : Seamless Instance Access

Import · Dec 10, 2023 · article

When aiming to synchronize data between instances, our primary focus naturally shifts to REST/SOAP API integration.

While going through from one of the requirement, I've explored a way that allows seamless data synchronization between different ServiceNow instances. It's a bit like using "GlideRecord," and it's called "GlideRemoteGlideRecord." This adds an interesting twist to how we can smoothly perform CRUD operations between instances.

Using "GlideRemoteGlideRecord," it is possible to connect to tables in other instances, allowing for the retrieval/creation/updation/deletion of records. Below are examples demonstrating these functionalities:

1) Reading Record From Remote Table:

var gRemote = new GlideRemoteGlideRecord('https://devXXXXX.service-now.com',’table_name’); // Param 1 is the servicenow instance of which you want to access the table, param 2 is the name of the table.
gRemote.setBasicAuth(‘user_name’,’password’); //authentication
gRemote.query(); 
while(gRemote.next()){ 
gs.info(“Value : ” + gRemote.getValue(‘field_name’));
}

2) Creating a new Record From Remote Table:

var gRemote = new GlideRemoteGlideRecord('https://devXXXXX.service-now.com',’table_name’); // Param 1 is the servicenow instance of which you want to access the table, param 2 is the name of the table.
gRemote.setBasicAuth(‘user_name’,’password’); //authentication
gRemote.initialize(); 
gRemote.setValue("field_name","field_value");  //Setting the values
gRemote.insert();

3) Updating Records in Remote Table:

var gRemote = new GlideRemoteGlideRecord('https://devXXXXX.service-now.com',’table_name’); // Param 1 is the servicenow instance of which you want to access the table, param 2 is the name of the table.
gRemote.setBasicAuth('user_name','password'); //authentication
gRemote.addQuery('field_name', 'field_value');
gRemote.query();
while (gRemote.next()) {
    gRemote.setValue('field_name', 'new_field_value');
    gRemote.update();
}

4) Deleting Records in Remote Table:

var gRemote = new GlideRemoteGlideRecord('https://devXXXXX.service-now.com',’table_name’); // Param 1 is the servicenow instance of which you want to access the table, param 2 is the name of the table.
gRemote.setBasicAuth('user_name','password'); 
gRemote.addQuery("field_name","field_value"); 
gRemote.query();
while (gRemote.next()) { 
gRemote.deleteRecord(); 
}


Happy Reading!! :smiling_face_with_smiling_eyes:

If you find the above article helpful , Please give me a :thumbs_up: on the post!!

View original source

https://www.servicenow.com/community/developer-articles/glideremotegliderecord-api-magic-seamless-instance-access/ta-p/2758006