Close a Change ticket with servicenow API?

dashap
Giga Contributor

I am trying to close a change ticket, but I cannot seem to get it to close.  The ticket is in draft, and I want to immediately close it with a change status as part of automation.

The issue is I cannot figure out how to close it.

 

Using:

response = requests.patch(sn_url+'/api/sn_chg_rest/v1/change/standard/' + sys_id, 
	data="{	\"u_close_code\": \"" + "1" + "\", \"u_change_stage\": \"" + "Completed" + "\", \"state\": \"3\" }", 
	headers=headers)
result_json = json.dumps(response.json(), indent=1)
print(result_json)

It throws:

{
 "error": {
  "message": "Invalid field.",
  "detail": "u_change_stage is read-only."
 },
 "status": "failure"
}

If I flip around the data in the rest call so that state is listed before u_change_stage, it says that state is read-only and invalid field.  

Is this the right way to try and close a ticket?  Is there some role issue going on?  Anybody seen this issue and know how to address?

1 ACCEPTED SOLUTION

dashap
Giga Contributor
I had to use another option called updateChange to get this done:

# Open Ticket
response = requests.put(sn_url+'/api/sn_chg_rest/change/updateChange', \
    data="{ \
        \"values\": { \
            \"change\": { \
                \"sys_id\": \"" + sys_id + "\", \
                \"state\":\"1\" \
            } \
        } \
    }", headers=headers)


I opened the ticket first with the previous json and then ran:

# Close Ticket
response = requests.put(sn_url+'/api/sn_chg_rest/change/updateChange', \
    data="{ \
        \"values\": { \
            \"change\": { \
                \"sys_id\": \"" + sys_id + "\", \
                \"state\":\"3\", \
                \"u_close_code\": \"1\" \
            } \
        } \
    }", headers=headers)

View solution in original post

2 REPLIES 2

emir
ServiceNow Employee
ServiceNow Employee

you have 2 custom fields that you have to look into (note they start with u_ )

try without those fields, and see if it works. it seems your custom fields are read only and the API can not update them.

dashap
Giga Contributor
I had to use another option called updateChange to get this done:

# Open Ticket
response = requests.put(sn_url+'/api/sn_chg_rest/change/updateChange', \
    data="{ \
        \"values\": { \
            \"change\": { \
                \"sys_id\": \"" + sys_id + "\", \
                \"state\":\"1\" \
            } \
        } \
    }", headers=headers)


I opened the ticket first with the previous json and then ran:

# Close Ticket
response = requests.put(sn_url+'/api/sn_chg_rest/change/updateChange', \
    data="{ \
        \"values\": { \
            \"change\": { \
                \"sys_id\": \"" + sys_id + "\", \
                \"state\":\"3\", \
                \"u_close_code\": \"1\" \
            } \
        } \
    }", headers=headers)