Odoo 11 ValueError: Expected singleton: %s\" % rec

2019-08-18 00:53发布

问题:

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)