Odoo通过API发送邮件从采购订单:记录不存在

huangapple go评论61阅读模式
英文:

Odoo send mail via API from Purchase Order : record does not exist

问题

在VPS上运行Odoo。

我尝试使用以下代码从已验证的采购订单发送邮件:

sendMail = models.execute_kw(db, uid, password, 'purchase.order', 'action_rfq_send', [orderId], {'context': {'send_rfq': False}})

models.execute_kw(db, uid, password, 'mail.compose.message', 'action_send_mail', [[sendMail['context']['default_template_id']]])

当手动触发时,邮件已配置并且正常工作。

我得到以下输出:Odoo通过API发送邮件从采购订单:记录不存在

第一个块是sendMail方法的输出。

第二个块是在xmlrpc的action_send_mail之后得到的输出。

我可能在sendMail或action_send_mail中漏掉了一个参数,但我找不到是哪个。我也尝试过用view_id替换default_template_id,代码如下:

models.execute_kw(db, uid, password, 'mail.compose.message', 'action_send_mail', [[sendMail['context']['view_id']]])
英文:

Running odoo on vps.

I Try to send mail from validated purchase order using :

sendMail = models.execute_kw(db, uid, password, 'purchase.order', 'action_rfq_send',[orderId],{'context' : {'send_rfq': False}})

models.execute_kw(db, uid, password, 'mail.compose.message', 'action_send_mail',[[sendMail['context']['default_template_id']]])

Mailing is configured and works when triggered manually

I get these outputs: Odoo通过API发送邮件从采购订单:记录不存在

First block is a print of the output of the sendMail method

Second block is the output of the xmlrpc action_send_mail that comes after.

I must be missing a parameter in either the sendMail or the action_send_mail but I couldn't find which. I have also tried replacing default_template_id by view_id as such:

models.execute_kw(db, uid, password, 'mail.compose.message', 'action_send_mail',[[sendMail['context']['view_id']]])

答案1

得分: 3

使用XML-RPC可以使用邮件向导,但我不会使用它。

但错误是这样的。您试图使用邮件向导的action_send_mail,它需要当前向导ID。但您提供的是[sendMail['context']['view_id']](或default_template_id),它不是向导ID。

purchase.orderaction_send_mail方法返回一个字典形式的操作。在Odoo窗口操作字典中,唯一的键/值对,持有操作底层模型的实际ID,是res_id,在这种情况下,它从未有过。那是因为Odoo为浏览器客户端设计了该方法...

那么你可以做什么呢?首先,您应该查看action_send_mail以了解其中的操作以及您可以使用的值。有很多东西适用于浏览器和向导。但是对于XML-RPC,只有两个东西是真正必要的:订单ID和邮件模板ID。

您已经有了订单ID,在您的示例代码中是orderId。对于Odoo 15,邮件向导使用两个邮件模板。让我们使用xml id purchase.email_template_edi_purchase对应的那个。

您现在可以使用这两个ID,通过Odoo的邮件模板功能发送邮件。

以下代码适用于Odoo 15,在旧版本中可能无法正常工作。

# 首先获取邮件模板ID(可以通过xml id获取,或者您可以硬编码,但那样不好看)
mail_template_xml_id = "purchase.email_template_edi_purchase"
module, xml_id_name = mail_template_xml_id.split(".")
template_model, template_id = models.execute_kw(
    db, uid, password, 'ir.model.data',
    'check_object_reference',
    [module, xml_id_name])
# 通过模板发送邮件
models.execute_kw(db, uid, password, template_model,
    'send_mail',
    [template_id, orderId])
英文:

Using the mail wizard by xml-rpc is possible, but i wouldn't use it.

But the error is the following. You're trying to use action_send_mail of the mail wizard, which needs the currents wizard id. But you're giving [sendMail['context']['view_id']] (or default_template_id) which is not a wizards ID.

The method action_send_mail of purchase.order is returning an action in form of a dictionary. The only key/value pair in an odoo window action dictionary holding an actual ID of the actions underlying model is res_id and in this case, there is never one in it. That's because Odoo made that method for the browser client...

So what can you do instead? First you should look into action_send_mail to get what's done there and what values you could use. There are a lot of things for the browser and the wizard. But for xml-rpc only 2 things are really necessary: the order ID and the mail template ID.

You already have the order id, in your example code orderId. For Odoo 15 there are two mail templates used by the wizard. Let's stick to the one with xml id purchase.email_template_edi_purchase, which will be used in my example code later on.

You now can use both IDs to send the mail by using the features of Odoo's mail templates.

That following code is for Odoo 15 and probably not working in older versions.

# first get mail template id (either by xml id or you could hardcode it, but that's ugly)
mail_template_xml_id = "purchase.email_template_edi_purchase"
module, xml_id_name = mail_template_xml_id.split(".")
template_model, template_id = models.execute_kw(
    db, uid, password, 'ir.model.data',
    'check_object_reference',
    [module, xml_id_name])
# send the mail by template
models.execute_kw(db, uid, password, template_model,
    'send_mail',
    [template_id, orderId])

答案2

得分: 0

感谢您的建议!

我将完成这个答案:上面的代码创建了邮件,但实际上并没有发送它(在Odoo 16上运行),我通过添加以下部分成功发送了邮件:

mailId = models.execute_kw(db, uid, password, template_model, 'send_mail', [template_id, orderId])
models.execute_kw(db, uid, password, 'mail.mail', 'send', [mailId])
英文:

Thanks for the tips !

I will complete this answer: The above code creates the mail but doesn't actually send it (running on Odoo 16), i managed to send the mail by adding the following bit :

mailId = models.execute_kw(db, uid, password, template_model, 'send_mail', [template_id, orderId]) 
models.execute_kw(db,uid,password, 'mail.mail', 'send', [mailId] ) 

huangapple
  • 本文由 发表于 2023年3月7日 19:47:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75661599.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定