rbw / pysnow

ServiceNow API Client Library
MIT License
203 stars 89 forks source link

How to Query and Update Journal Tables - WorkNotes #112

Closed dfields186 closed 5 years ago

dfields186 commented 5 years ago

I need the ability to query and update the sys journal tables. I would like the ability to query the journal table to retrieve a list of work notes and/or comments associated with an incident ticket. I also would like the ability to post an updated work note to an incident ticket. Perhaps these capabilities are already available - but its not clear to me how to go about doing it. Any help is greatly appreciated!

Thanks in advance! David

rbw commented 5 years ago

In order to join and retrieve comments and work_notes, you'll need to set Resource.parameters to either "all" or "True".

incidents = c.resource(api_path="/table/incident")
incidents.parameters.display_value = "all"

However, this only returns unstructured/raw strings (no separation of messages and usernames for instance).

Adding a new comment or work note is easy:

incidents = c.resource(api_path="/table/incident")
incidents.update({"number": "<number>", {"comments": "hello!"})

Custom API

AFAIK, if you want to work with comments / work_notes in a more structured manner - you'll need to create a custom ServiceNow API. I've implemented this for Task (which Incident normally inherits from). Check out this Gist for an example: https://gist.github.com/rbw/5b44842a6626eeaf2b024a59d93752d1

Note: There might be less complex ways to archive this that I'm unaware of.

dfields186 commented 5 years ago

Robert, that worked! I did have to add an ending brace - looked like you accidentally left it out of your example right after number - corrected line below:

incidents.update({"number": "<number>"}, {"comments": "hello!"})

So here was my test code, and I was able to print out as well as add new comments and work notes to an existing incident ticket - this works just exactly as I need it to. Being a text string, I can easily work with it using regex.

import pysnow conn = pysnow.Client(instance=myinstance, user=userID, password=mypassword') incidents = conn.resource(api_path="/table/incident") incidents.parameters.display_value = "all" # OR incidents.parameters.display_value = True

response = incidents.get(query={'number': inc_number})

import json print(json.dumps(response.one(), indent=2, sort_keys=True))

print(response.one()['work_notes']) print(response.one()['comments']) print(response.one()['comments_and_work_notes'])

updated_incident = incidents.update({'number': inc_number}, {"comments": "Commenting on this incident"}) updated_incident = incidents.update({"number": inc_number}, {"work_notes": "Adding a new work note to this incident"})

response = incidents.get(query={'number': inc_number) print(response.one()['comments']) print(response.one()['work_notes'])

Thank you very much Robert!

dfields186 commented 5 years ago

btw Robert: I looked at the task API code and need to study it much more to try and better understand whats going on and if it could solve any of my requirements - thank you! https://gist.github.com/rbw/5b44842a6626eeaf2b024a59d93752d1

dfields186 commented 5 years ago

Robert - along with worknotes and comments, is there a way to print all of the other various journal entries associated with an incident ticket - in addition to comments and work notes this would also include Field Changes, Emails Sent, Image Uploaded, etc.?

Also noticed depending on the flag used with the 'display_value' the output format is quite different - "all" produces more of a JSON-style python dictionary result whereas the "True" parameter products a result which prints out nicely:

incidents.parameters.display_value = "all" # OR incidents.parameters.display_value = True

rbw commented 5 years ago

Sure, no problem :)

Robert - along with worknotes and comments, is there a way to print all of the other various journal entries associated with an incident ticket - in addition to comments and work notes this would also include Field Changes, Emails Sent, Image Uploaded, etc.?

Not that I know of. Some of this information is contained in "events" property of the custom API that I linked to.

Also noticed depending on the flag used with the 'display_value' the output format is quite different - "all" produces more of a JSON-style python dictionary result whereas the "True" parameter products a result which prints out nicely:

all can be useful when you're interested in a related table's Sysid and Name, instead of just Name.