Search code examples
jsonpython-3.xodooxmlrpcclientodoo-18

Create Log note from client xmlrpc in odoo18


I am trying to create log notes through client xmlprc library from reading log notes through json file in helpdesk module but stuck with an typeError named positional arguments not as expected. here is the error:-

TypeError: MailThread.message_post takes 1 positional argument but 3 were given.

Below is my code:

import json
import xmlrpc.client

db18 = 'test'  # write your database
user18 = 'admin'  # write your user
password18 = 'admin'
url18 = 'http://localhost:8018'
common18 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/common'.format(url18))
uid18 = common18.authenticate(db18, user18, password18, {})
print("uid::::::::::::::::::::::", url18)
models18 = xmlrpc.client.ServerProxy('{}/xmlrpc/2/object'.format(url18), 
allow_none=True)
print("models:::::::::::::::::::::::::", models18)

# Open the JSON file and load the data
with open('Tickets0.json', 'r') as file:
    data = json.load(file)



for res in data:
    # print(res)
    description_html = res['helpdesk_ticket']['description_html']
    # print("id::::::::", description_html)

   helpdesk = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket', 
'search', [[['helpdesk_id', '=', helpdesk_id]]])
   print("helpdesk::::::::::::::::", helpdesk)
   if helpdesk:
       print("in if:::::::::::::::::::::", helpdesk[0])
       log_note = "Ticket updated from Freshdesk with Subject"
       try:
        
           doc_id = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket', 
       'message_post', [[helpdesk[0]], description_html, {}])
           print(f"Record updated with ID: {doc_id}")
       except Exception as e:
           print(f"Error creating record: {e}")

I don't know how to add record id of helpdesk ticket when calling method. I have tried giving it after method like below:-

doc_id = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket', 
       'message_post', [helpdesk[0]],[description_html])

But didn't work and also searched some of the post but didn't worked anything.

Is there anything I am doing incorrect or anything I am missing?


Solution

  • execute_kw usually needs 7 arguments:

    1. db name
    2. logged in user id
    3. user password
    4. model name
    5. method name
    6. method positional arguments as list
    7. method keyword arguments as dictionary

    mail.thread.message_post() (or helpdesk.ticket.message_post()) takes exactly one positional argument (for the external API it's the id to get a recordset into self), but you have 3 in your list.

    Following should work:

    doc_id = models18.execute_kw(db18, uid18, password18, 'helpdesk.ticket', 
           'message_post', [helpdesk[0]], {'body': description_html})