2016-07-26 42 views
0

我有以下代碼。當調度程序運行時,我收到錯誤消息。有人幫我解決代碼中的錯誤Odoo運行發送預定郵件的cron作業時丟失錯誤

MissingError 您試圖訪問的文件之一已被刪除,請刷新後再試一次。

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 
+0

這就是您從Odoo獲得的所有錯誤消息嗎?似乎你正在嘗試使用那部分代碼的一些記錄,現在已經不在db了。 – CZoellner

+0

我正面對這一行的錯誤 - mail_id = self.pool.get('email.template')。send_mail(cr,uid,email_template_line.id,1,force_send = True,context = context) – sfx

+0

該參數' 1'告訴Odoo使用模板中定義的模型的ID爲1的記錄用於值表達式。似乎數據庫中沒有該特定模型的ID爲1的記錄。爲什麼該參數設置爲1?它不應該是'quot_id'嗎? – CZoellner

回答

0

這是修改過的代碼並且工作正常

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 
0

的使用新的API所有寫代碼的第一位。

爲了得到模板使用obj = self.env.ref('template_record_id')然後發送obj.send_mail(model.obj.id, force_send=True),如果你想設置郵件再發送前obj.email_to = '[email protected]'

最終代碼:

某處在XML:

<record id="email_template_record_id" model="email.template"> 
     <field name="name">Name</field> 
     <field name="email_from">[email protected]</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 
+0

我支持你使用新api的意圖,但爲什麼你應該在每次調用時都設置template.email_to?這將改變數據庫中的模板記錄。 Odoo的電子郵件模板支持佔位符/值表達式。該電子郵件應該從模板用於的記錄中獲取。 – CZoellner

+0

我現在只有這個工作解決方案。無論如何感謝您的更正 –

+0

@CZoellner我有問題,我的解決方案是立即發送電子郵件,並且send_email沒有電子郵件的參數,它從模板中獲取它,這就是爲什麼我要爲其分配電子郵件。但在上面的例子中,它將郵件添加到池中,並且它的性能是否更好? –

相關問題