Odoo 11 ValueError: Expected singleton: %s" % record

亡梦爱人 提交于 2019-12-24 19:10:14

问题


I am creating a custom module where I want to get all the selected employee in the Payroll and send them a default email. For that I am doing this

To show the action button

<?xml version="1.0" encoding="utf-8"?>
<odoo>
    <data>
      <record id="action_email_payslip" model="ir.actions.server">
      <field name="name">Email</field>
      <field eval="2" name="sequence"/>
      <field name="view_mode">form</field>
      <field name="multi" eval="False"/>
            <field name="model_id" ref="hr_payroll.model_hr_payslip"/> 
            <field name="binding_model_id" ref="hr_payroll.model_hr_payslip"/>
            <field name="state">code</field> 
            <field name="code"> 
            action = records.action_send_email() 
            </field>
    </record>
    </data>
</odoo>

In the model my code is like this

from odoo import models, fields, api, _

class EmailPayslip(models.Model):
    _inherit = 'hr.payslip'

    @api.multi
    def action_send_email(self):

        selected_employess = []
        not_selected_employees = []

        for payslip in self:
          try:
            lang = payslip.employee_id.user_id.lang
            template.with_context(lang=lang).send_mail(
                self.env.user.id, force_send=True, raise_exception=True
            )
            selected_employess.append(payslip.name)
          except Exception as e:
            not_selected_employees.append(payslip.name)

        print(selected_employess)

But its showing error like

ValueError: Expected singleton: %s" % record

ValueError: Expected singleton: hr.payslip(1, 3, 4)

Can someone tell me what I a doing wrong here and how to fix this? Any help and suggestions will be really appreciable.

Regards,


回答1:


Try the below code snippet,

 template.with_context(lang=lang).send_mail(
            payslip.employee_id.user_id.id, force_send=True, raise_exception=True
        ) 



回答2:


Try to loop the 'payslip' also , like below

 for payslip in self:
    for slip in payslip:
         try:
           lang = payslip.employee_id.user_id.lang
           template.with_context(lang=lang).send_mail(
               self.env.user.id, force_send=True, raise_exception=True
           )
           selected_employess.append(slip.name)
         except Exception as e:
           not_selected_employees.append(slip.name)


来源:https://stackoverflow.com/questions/56113613/odoo-11-valueerror-expected-singleton-s-record

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