This question has been flagged
1 Reply
4532 Views

I'm trying to get a field from openERPs mail_message model using python code which is executed in a `server action` (so its not a module where I can debug! I cannot even `print` in this state) (when a new eMail is being fetched) but I am unable to get anything useful from it.

Basicly when someone is throwing me a email, a new Task is created by openERP. But the newely created ticket is not connected to the user which send me the mail.

When a new email is fetched, this server action gets executed.

In a table called mail_message you can then find the email (+ author_id, + email, + res_id (which is the id of the created Task), therefore I'd like to fetch the author_id from that table.
(A query would look like this:
`SELECT author_id FROM mail_message WHERE type = 'email' AND res_id = '<Task.id>'`)

This is my current code


    #Initialize object. That one points to the mail_message model.
    mailMessage_obj = self.pool.get('mail.message')
    
    #Created Id in project_task
    myId = object.id
    
    #browse whole object with that id
    #message = mailMessage_obj.browse(cr,uid,[myId])
    
    #Select field where 
    messageIds = mailMessage_obj.search(cr,uid,[('type','=','email'),('res_id','=',myId)],context=context)

    if messageIds:
        #messageRecord = mailMessage_obj.browse(cr,uid,[myId],context=context)
        #object.write({'partner_id':messageRecord.author_id.id})
    
        res = mailMessage_obj.read(messageIds, ['author_id'])
        partnerId = res[0]
    
        #Author id
        #partnerId = message[0]['author_id']
        #partnerId = message.author_id
    
    
        #res = [(r['id'], r['author_id']) for r in messageRecord]
    
        #partnerId = res
    
        #partnerId = 259866
        object.write({'partner_id':partnerId})

I dont know how to get my hands on the author_id properly. If I hardcode a ID and let it write to the database (last two lines) It'll work just fine, but I cant hardcode a users id. ;)

Could someone explain to me how its done correctly?
I dont know whether I should use .browse or .read or something else..

Avatar
Discard
Author

@David your code throws "AttributeError: 'browse_record_list' object has no attribute 'author_id'" object.id returns the Taskid that has been created (Not 100% sure of it, but its the best guess I currently have)

Best Answer

I assume object.id is something exogenous, in the scope of the information you have given. This might answer one of your question:

partnerid = mailMessage_obj.browse(cr,uid,[myId],context=context).author_id

or

message = mailMessage_obj.browse(cr,uid,[myId],context=context)
partnerid = message.author_id

Avatar
Discard