Update incident in service now using Python

Piyush36
Kilo Explorer

Hi All,

I am trying to post worknotes(additional_comments) on an incident using below in a python script. I have tried the same with Request Item and was able to do so.

 

I have tried this with request item (sc_task)

def postComments(sctaskID, notes):
    uname, pwd = X,Y
    url = '{0}processRequestTask.do'.format(ServiceNowBaseURL)

    headers = {"Content-Type":"application/xml", "Accept":"application/xml"}
    content = "<request><task_id>" + sctaskID + "</task_id><additional_comments>"+ notes + "</additional_comments></request>"
    logging.info(content)
    r = requests.post(url, data=content, auth=(uname, pwd), headers=headers)
    
    responseCode = getResponseCode(r)
    if responseCode != "0": 
        logging.error(r.text)
        return None
    return "Success"

 

What will be used for incident (inplace of processRequestTask.do).. i have tried incident.do & processincident.do but it doesn't work.

Is there any way we can find what URL is used in service now to process an incident while doing an API call.

If I try: https://instance.service-now.com/incident.do --> it leads to create new incident page. I wish to update the additional comments in the incident.

Any help is greatly appreciated!! Thanks

5 REPLIES 5

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Piyush,

You can use below sample code.

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://instanceName.service-now.com/api/now/table/incident/sys_idOfIncident'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.patch(url, auth=(user, pwd), headers=headers )

# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

 

Hope it helps.

Regards,

Ujjawal

Updated with comments field

#Need to install requests package for python
#easy_install requests
import requests

# Set the request parameters
url = 'https://instance.service-now.com/api/now/table/incident/sys_idOFIncident'

# Eg. User name="admin", Password="admin" for this code sample.
user = 'admin'
pwd = 'admin'

# Set proper headers
headers = {"Content-Type":"application/json","Accept":"application/json"}

# Do the HTTP request
response = requests.patch(url, auth=(user, pwd), headers=headers ,data="{\"comments\":\"something\"}")

# Check for HTTP codes other than 200
if response.status_code != 200: 
    print('Status:', response.status_code, 'Headers:', response.headers, 'Error Response:',response.json())
    exit()

# Decode the JSON response into a dictionary and use the data
data = response.json()
print(data)

Hi Piyush,

If I helped you to resolve your query with my answer. Then I would really appreciate if you can mark my answer correct and helpful. So that it can be moved to answered list and helpful for future readers.

Regards,
Ujjawal

Anil Lande
Kilo Patron

Hi,

Please find the samples and documentations on below link.

 

https://docs.servicenow.com/bundle/quebec-application-development/page/integrate/inbound-rest/concep...

 

Thanks,

Anil Lande

Please appreciate the efforts of community contributors by marking appropriate response as correct answer and helpful, this may help other community users to follow correct solution in future.
Thanks
Anil Lande