Search code examples
odoo-14

How do I set a value on a selection via a button


I need to create a button which will define a ticket the value of the state variable to cloturé.

And when a ticket is cloturé, it will on read only.

How can i do it ?

The ticket's model is helpdesk.ticket

view.xml :

<?xml version='1.0' encoding='UTF-8'?>
<odoo>
    <record id="helpdesk_ticket_view_form_inherit_header_modifie" model="ir.ui.view">
        <field name="name">helpdesk.ticket.modifie.header</field>
        <field name="model">helpdesk.ticket</field>
        <field name="inherit_id" ref="helpdesk_fsm.helpdesk_ticket_view_form" />
        <field name="arch" type="xml">
            <xpath expr="//button[@name='action_generate_fsm_task']" position="attributes" >
                <attribute name="string">Planifier tache</attribute>
            </xpath>
        </field>
    </record>
</odoo>

ticket.py :

# -*- coding: utf-8 -*-

from odoo import models, fields, api

class ticket_inherit(models.Model):
    _inherit = "helpdesk.ticket"
    
    state = fields.Selection(['test','annulé','cloturé'],'selection')
    
    #What i want to do
    def cloture_le_ticket(self):
        state = 'cloturé'
    

Solution

  • You can just use write().

    state = fields.Selection(string="State", selection=[
        ('test', 'Test),
        ('annulé', 'Annulé'),
        ('cloturé', 'Cloturé')
    ])
    
    def cloture_le_ticket(self):
        self.write({
            'state': 'cloturé'
        })
    

    To force the ticket to be read-only:

    def write(self, vals):
        for record in self:
            if record.state == 'cloturé':
                raise exceptions.UserError("Sorry but the ticket is read only")
        return super(HelpdeskTicket, self).write(vals)