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

China☆狼群 提交于 2019-12-12 03:45:14

问题


I have the following code. When the scheduler runs I am getting the error message. Some one help me to resolve the error in the code

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

回答1:


First of all write code using new api.

For getting template use obj = self.env.ref('template_record_id') then send obj.send_mail(model.obj.id, force_send=True), if you want to set mail then before send obj.email_to = 'test@example.com'

Final code:

Somewhere in 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>

in python code:

    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



回答2:


This is the modified code and works fine

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


来源:https://stackoverflow.com/questions/38583288/odoo-missing-error-while-running-a-cron-job-for-sending-scheduled-mail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!