运行 用于发送预定邮件的 cron 作业时 Odoo 丢失错误

Odoo Missing Error while running a cron job for sending scheduled mail

我有以下代码。当调度程序运行时,我收到错误消息。有人帮我解决了代码中的错误

MissingError One of the documents you are trying to access has been deleted, please try again after refreshing.

def send_followup_mail(self, cr, uid, context=None):
        quot_ids=self.search(cr, uid, [('state','=','amend_quote')])
        for quot_id in quot_ids:
            if quot_id:
                quot_obj=self.browse(cr, uid, quot_id ,context=context)
                quotation_since=quot_obj.quotation_since
                for email_template_line in quot_obj.temp_tag_id.crm_campaign_id.email_template_ids:
                    if quotation_since==email_template_line.delay_days:
                        mail_pool = self.pool.get('mail.mail')
                        mail_id = self.pool.get('email.template').send_mail(cr, uid, email_template_line.id, 1, force_send=True, context=context)

                        if email_template_line.send_mail_to=='to_client':
                            mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.email_from}, context=context)

                        elif email_template_line.send_mail_to=='to_sale_rep':
                            mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.sale_rep_id.email}, context=context)

                        if mail_id:
                            mail_pool.send(cr, uid, mail_id, context=context)
                self.write(cr, uid, quot_id,{'quotation_since':quotation_since+1}, context=None)
        return True

首先使用new api.

编写代码

获取模板使用obj = self.env.ref('template_record_id')然后发送obj.send_mail(model.obj.id, force_send=True),如果你想设置邮件然后再发送obj.email_to = 'test@example.com'

最终代码:

xml 中的某处:

    <record id="email_template_record_id" model="email.template">
        <field name="name">Name</field>
        <field name="email_from">noreply@example.com</field>
        <field name="subject">Subject</field>
        <field name="email_to">False</field>
        <field name="auto_delete" eval="True"/>
        <field name="model_id" ref="model_model_name"/>
        <field name="body_html">
            <![CDATA[
            <html>
                <head>
                    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
                    <title>Custom title</title>
                </head>
                <body>
                    <p>Dear Sir,</p>
                    <p>Text body.</p>
                    <p>This is automated mail, don't reply.</p>
                </body>
            </html>
            ]]>
        </field>
    </record>

在python代码中:

    template = self.env.ref('email_template_record_id')
    template.email_to = some_obj.obj_related_field.email
    try:
        template.send_mail(ombject_id, force_send=True)
    except Exception:
        _logger.error("Custom Error Message")
    template.email_to = False

这是修改后的代码,可以正常工作

def send_followup_mail(self, cr, uid, context=None):
            quot_ids=self.search(cr, uid, [('state','=','amend_quote')])
            for quot_id in quot_ids:
                if quot_id:
                    quot_obj=self.browse(cr, uid, quot_id ,context=context)
                    quotation_since=quot_obj.quotation_since
                    for email_template_line in quot_obj.temp_tag_id.crm_campaign_id.email_template_ids:
                        if quotation_since==email_template_line.delay_days:
                            mail_pool = self.pool.get('mail.mail')
                            template_id = email_template_line.id
                            template_pool = self.pool.get('email.template')
                            sale_id=self.pool.get('sale.order').search(cr, uid, [], limit=1, context=context)
                            mail_id = template_pool.send_mail(cr, uid, template_id, sale_id[0], force_send=True, context=context)

                            if email_template_line.send_mail_to=='to_client':
                                mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.email_from}, context=context)

                            elif email_template_line.send_mail_to=='to_sale_rep':
                                mail_pool.write(cr, uid, mail_id, {'email_to':quot_obj.sale_rep_id.email}, context=context)

                            if mail_id:
                                mail_pool.send(cr, uid, mail_id, context=context)
                    self.write(cr, uid, quot_id,{'quotation_since':quotation_since+1}, context=None)
            return True