pythonodooodoo-8

Create a new record in another model with a condition in Odoo v8


I have this model

class crm_claim(models.Model):
    _inherit = 'crm.claim'

    needs_credit_note = fields.Boolean(string="Needs Credit Note?")
    partner_id = fields.Many2one('res.partner', string='Partner')

I need to create a function that, when needs_credit_note == True,creates a new record con account.invoice with the same partner_id, and when needs_credit_note == False, delete this record. This is what I did but is not working yet.

@api.depends("needs_credit_note")
def create(self, vals):
    self.ensure_one()
    if self.needs_credit_note:
        self.env['account.invoice'].create({
            'partner_id': self.partner_id.id,
        })

Solution

  • You can't do that, self is an empty record set, and self.needs_credit_note will be evaluated to False. You need to call super or get the values from vals.

    The partner field should be required when the needs_credit_note field value is set

    Example:

    @api.model
    def create(self, vals):
        record = super(crm_claim, self).create(vals)
        if record.needs_credit_note:
            self.env['account.invoice'].create({
                'partner_id': record.partner_id.id,
            })
        return record
    

    Probably, you will need to provide the required fields to create the invoice record