Reputation: 635
I'm trying to add "Affected CIs" to a problem ticket in ServiceNow:
Using some python automation, I'm able to create several tickets using the following:
import pysnow
snow = pysnow.Client(instance=instance, user=username, password=password, raise_on_empty=True)
snow.table = 'problem'
affected_ci = 'jbj9842jm'
payload = {'short_description':ticket.title,
'description':ticket.verbiage,
'assignment_group':last_rec['support_group'],
'assigned_to':last_rec['support_group_manager'],
'u_root_cause':ticket.root_cause,
'u_vulnerability_type':ticket.v_type,
'u_vulnerability_criticality':ticket.v_criticality,
'urgency':ticket.v_urgency,
'impact':ticket.v_impact,
'u_problem_notes':'Affected CIs\n'+affected_ci
}
res = snow.insert(table='problem', payload=payload)
print("Support Group: "+payload['assignment_group']+" has been assigned ticket number: "+res['number'])
But, to this point, I have only been able to add "Affected CIs" as problem notes 'u_problem_notes':'Affected CIs\n'+affected_ci
.
I know the "Affected CIs" table is a table attached to the problem ticket record. What I don't know is how to add an item (or items) to this table.
Seeing as the problem ticket is contained in the "problem" table, I thought I might access it by using the request from the original insert
to get the problem ticket number, then add that to the table path and insert a CI there:
ci = payload['u_problem_notes'].split()
for i in range(1, len(ci)):
ci_payload = {'task_ci':ci[i]}
snow.insert(table='problem/'+res['number'], payload=ci_payload)
That yielded the following:
pysnow.legacy_exceptions.UnexpectedResponse: Unexpected HTTP POST response code. Expected 201, got 405
How do I add "Affected CIs" to a ServiceNow problem ticket using pysnow?
Upvotes: 0
Views: 1804
Reputation: 635
I figured it out.
The "Affected CIs" table is actually the task_ci
table in ServiceNow. To post a record to it, you need to provide the sys_id
of both the CI you're trying to add, and the PRB ticket you're submitting it to.
To get the CI sys_id
, you need to query whatever table your company uses to store configuration items, in my case the cmdb_ci
table.
Using the same pysnow client as above, you can create two more "resources", which are REST API call wrappers that point to the appropriate tables:
q_ci = snow.resource(api_path='/table/cmdb_ci')
t_ci = snow.resource(api_path='/table/task_ci')
Then loop through your list of CIs to first check that they exists in the CMDB, then add them to the task_ci
table:
# Look up CIs in CMDB and add to the Affected CI table for each PRB ticket
for ci in ci_list: # List of CI hostnames
try:
# Ask CMDB if hostname exists
ci_info = q_ci.get(query={'name': ci})
if ci_info == '':
raise ValueError
# If it does, create a task_id record for that PRB ticket
# With 'res' being the response from the PRB ticket insert call
t_ci.create({'ci_item':ci_info['sys_id'], 'task':res['sys_id']})
except:
# Otherwise, make a note of unknown hostnames
not_found_in_cmdb.append({'ci_name':ci, 'for_prb':res['number']})
continue
Upvotes: 0