How to create sc_task integration on both instance?

Bhagyshri Chava
Tera Contributor

I'm having a task.

I have a requirement of integrating these 2 ServiceNow instances for service request:

instance 1                   instance 2

req    -->                         req

ritm   -->                         ritm

task   -->                         task

does any body having a script to complete this requirement and how to do it? and what NameSpace and TABLE do we use in Rest API Explorer.And can anybody have a demo script?

2 REPLIES 2

piyushsain
Tera Guru
Tera Guru

Hi,

Create an insert BR on each table of instance1. I am showing you the example for request table. Get the data of the inserted record by the Table API of the same instance, you need to add admin id and password. After getting the data send that JSON to the second instance through POST table API.

 

piyushsain_0-1691414982845.png

 

 

If my answer has helped with your question, please mark my answer as accepted solution and give a thumb up.
Regards,
Piyush Sain

Tushar
Kilo Sage
Kilo Sage

Hi @Bhagyshri Chava 

 

something like this should help here -

 

import requests

# Instance 1 details
instance1_url = "https://instance1.service-now.com"
instance1_username = "your_username"
instance1_password = "your_password"

# Instance 2 details
instance2_url = "https://instance2.service-now.com"
instance2_username = "your_username"
instance2_password = "your_password"

# API endpoint
api_endpoint = "/api/now/table/"

# Function to copy records from one instance to another with duplicate check
def copy_records(source_instance, source_table, target_instance, target_table):
    headers = {"Content-Type": "application/json"}

    # Fetch records from source instance that do not exist in the target table
    source_records_url = instance1_url + api_endpoint + source_table
    target_records_url = instance2_url + api_endpoint + target_table
    response = requests.get(source_records_url, auth=(instance1_username, instance1_password), headers=headers,
                            params={"sysparm_query": "sys_idNOT IN (SELECT sys_id FROM " + target_table + " IN " + target_instance + ")"})
    records = response.json()["result"]

    # Create records in target instance
    for record in records:
        response = requests.post(target_records_url, auth=(instance2_username, instance2_password), headers=headers, json=record)
        print("Record copied:", response.status_code)

# Copy records from Instance 1 to Instance 2
copy_records("instance1", "req", "instance2", "req")
copy_records("instance1", "ritm", "instance2", "ritm")
copy_records("instance1", "task", "instance2", "task")

 

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Regards,
Tushar