how to create incident using rest

Priti18
Tera Expert

how can I create incident from one instance and it should get created in another instance too using rest api.

can anyone give me step by step detailed explanation for this requirement

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Priti18 

Refer below link for step by step instructions

ServiceNow to ServiceNow Integration using REST API and PUSH and PUT methods with Business Rule 

You can also explore on Ebonding Spoke with less scripting

ServiceNow eBonding spoke 

Refer the below link which has the approach with example shared by me.

Integration hub ebonding spoke 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

3 REPLIES 3

Kamil Smusz
Kilo Sage

hi @Priti18 ,

 

it's possible. So you will need Business rule on Insert, in script you need to call Rest Message to another instance using ServiceNow Table API.
Rest message need to be post to another instance like that https://your_instance.service-now.com/api/now/table/incident and in script you need to build JSON payload example below and send it as body

var body = '{"short_description": "'+current.short_description+'","description": "'+current.description+'","priority": "'+current.priority+'"}';

 

First, make sure you have the 'requests' library installed:

 

pip install requests

 

 

Now, follow these steps:

  1. Import necessary libraries:

 

import requests
import json

 

  • Define your instances, authentication, and API endpoint information:

 

instance_1 = "https://instance1.service-now.com"
instance_2 = "https://instance2.service-now.com"

api_endpoint = "/api/now/table/incident"

username_1 = "your_instance1_username"
password_1 = "your_instance1_password"

username_2 = "your_instance2_username"
password_2 = "your_instance2_password"

 

  •  

    1. Define the incident details (use your own data as needed):

     

     

 

incident_data = {
    "short_description": "Sample incident",
    "category": "software",
    "subcategory": "email",
    "urgency": "2",
    "impact": "2",
}

 

  1. Create a function to create an incident in a specific instance:

 

def create_incident(instance, username, password, data):
    headers = {
        "Content-Type": "application/json",
        "Accept": "application/json",
    }
    
    auth = (username, password)
    url = instance + api_endpoint

    response = requests.post(url, auth=auth, headers=headers, data=json.dumps(data))

    if response.status_code == 201:
        print(f"Incident created successfully in {instance}")
        return json.loads(response.content)
    else:
        print(f"Error creating incident in {instance}: {response.status_code} - {response.content}")
        return None

 

  • Call the function to create incidents in both instances:

 

incident_1 = create_incident(instance_1, username_1, password_1, incident_data)
incident_2 = create_incident(instance_2, username_2, password_2, incident_data)

 

This code snippet should create an incident in both instances with the specified details. Make sure to replace the instance URLs, usernames, passwords, and incident_data with your own information. If the incidents are created successfully, the incident_1 and incident_2 variables will hold the created incident data in JSON format for each instance, respectively. If there's an error, the error message and status code will be printed.

Ankur Bawiskar
Tera Patron
Tera Patron

@Priti18 

Refer below link for step by step instructions

ServiceNow to ServiceNow Integration using REST API and PUSH and PUT methods with Business Rule 

You can also explore on Ebonding Spoke with less scripting

ServiceNow eBonding spoke 

Refer the below link which has the approach with example shared by me.

Integration hub ebonding spoke 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader