markciecior / ConnectPyse

ConnectWise (Manage) REST API client written in Python 3.x
MIT License
21 stars 8 forks source link

Issue with Upload Document for Ticket #19

Closed nyteghost closed 2 years ago

nyteghost commented 2 years ago

I am trying to upload a document to a ticket but keep getting error; "raise Exception('Document creation only supported for Tickets, Opportunities, and Companies')" o = tickets_api.TicketsAPI(url=cw.cwURL, auth=cw.cwAUTH) d = document_api.DocumentAPI(url=cw.cwURL, auth=cw.cwAUTH) file_name = '{}.xlsx'.format(idName) file_location=r'{}'.format(file_name) a_ticket = o.get_ticket_by_id(ticket_id=i) f = os.path.join(os.path.curdir, file_location) a_document = d.create_document(o, 'Newly Uploaded Document', file_name, open(f, 'rb')) print(a_document.title)

emichaud commented 2 years ago

hey @nyteghost,

Your issue was that you didn't actually provide an object to the api. You passed in the variable "o" - which is only a reference to the api. You need to pass in an actual ticket object, which is what you got and stored in the variable "a_ticket"

To correct your issue: change a_document = d.create_document(o, 'Newly Uploaded Document', file_name, open(f, 'rb'))

to this: a_document = d.create_document(a_ticket , 'Newly Uploaded Document', file_name, open(f, 'rb'))

and it should work.

Here is a quick tip for you. If you already have the ticket id, then you can use a fake object as long as you add the correct id to it.

Instead of getting ticket object from the server this way:

api = tickets_api.TicketsAPI(url=BASE_URL, auth=AUTH) ticket = api.get_ticket_by_id(1190368)

fake ticket object with an id.

ticket = ticket.Ticket({'id':1190368})

here is an example:

ticket = ticket.Ticket({'id':1190368}) file_title = "List of files needed for this ticket" file_name = '/tmp/files.txt' file_data = open(file_name, 'rb') #this captures the content of the file into a variable - beware of large files doc = document_api.DocumentAPI(url=BASE_URL, auth=AUTH) a_document = doc.create_document(ticket, file_title, file_name, file_data)

best of luck!

nyteghost commented 2 years ago

Thank you for your help. That worked, and I learned =D