Skip to Content
Odoo Menú
  • Registra entrada
  • Prova-ho gratis
  • Aplicacions
    Finances
    • Comptabilitat
    • Facturació
    • Despeses
    • Full de càlcul (IA)
    • Documents
    • Signatura
    Vendes
    • CRM
    • Vendes
    • Punt de venda per a botigues
    • Punt de venda per a restaurants
    • Subscripcions
    • Lloguer
    Imatges de llocs web
    • Creació de llocs web
    • Comerç electrònic
    • Blog
    • Fòrum
    • Xat en directe
    • Aprenentatge en línia
    Cadena de subministrament
    • Inventari
    • Fabricació
    • PLM
    • Compres
    • Manteniment
    • Qualitat
    Recursos humans
    • Empleats
    • Reclutament
    • Absències
    • Avaluacions
    • Recomanacions
    • Flota
    Màrqueting
    • Màrqueting Social
    • Màrqueting per correu electrònic
    • Màrqueting per SMS
    • Esdeveniments
    • Automatització del màrqueting
    • Enquestes
    Serveis
    • Projectes
    • Fulls d'hores
    • Servei de camp
    • Suport
    • Planificació
    • Cites
    Productivitat
    • Converses
    • Validacions
    • IoT
    • VoIP
    • Coneixements
    • WhatsApp
    Aplicacions de tercers Odoo Studio Plataforma d'Odoo al núvol
  • Sectors
    Comerç al detall
    • Llibreria
    • Botiga de roba
    • Botiga de mobles
    • Botiga d'ultramarins
    • Ferreteria
    • Botiga de joguines
    Food & Hospitality
    • Bar i pub
    • Restaurant
    • Menjar ràpid
    • Guest House
    • Distribuïdor de begudes
    • Hotel
    Immobiliari
    • Agència immobiliària
    • Estudi d'arquitectura
    • Construcció
    • Gestió immobiliària
    • Jardineria
    • Associació de propietaris de béns immobles
    Consultoria
    • Empresa comptable
    • Partner d'Odoo
    • Agència de màrqueting
    • Bufet d'advocats
    • Captació de talent
    • Auditoria i certificació
    Fabricació
    • Textile
    • Metal
    • Mobles
    • Menjar
    • Brewery
    • Regals corporatius
    Salut i fitness
    • Club d'esport
    • Òptica
    • Centre de fitness
    • Especialistes en benestar
    • Farmàcia
    • Perruqueria
    Trades
    • Servei de manteniment
    • Hardware i suport informàtic
    • Sistemes d'energia solar
    • Shoe Maker
    • Serveis de neteja
    • Instal·lacions HVAC
    Altres
    • Nonprofit Organization
    • Agència del medi ambient
    • Lloguer de panells publicitaris
    • Fotografia
    • Lloguer de bicicletes
    • Distribuïdors de programari
    Browse all Industries
  • Comunitat
    Aprèn
    • Tutorials
    • Documentació
    • Certificacions
    • Formació
    • Blog
    • Pòdcast
    Potenciar l'educació
    • Programa educatiu
    • Scale-Up! El joc empresarial
    • Visita Odoo
    Obtindre el programari
    • Descarregar
    • Comparar edicions
    • Novetats de les versions
    Col·laborar
    • GitHub
    • Fòrum
    • Esdeveniments
    • Traduccions
    • Converteix-te en partner
    • Services for Partners
    • Registra la teva empresa comptable
    Obtindre els serveis
    • Troba un partner
    • Troba un comptable
    • Contacta amb un expert
    • Serveis d'implementació
    • Referències del client
    • Suport
    • Actualitzacions
    Github Youtube Twitter Linkedin Instagram Facebook Spotify
    +1 (650) 691-3277
    Programar una demo
  • Preus
  • Ajuda

Odoo is the world's easiest all-in-one management software.
It includes hundreds of business apps:

  • CRM
  • e-Commerce
  • Comptabilitat
  • Inventari
  • PoS
  • Projectes
  • MRP
All apps
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
You need to be registered to interact with the community.
All Posts People Badges
Etiquetes (View all)
odoo accounting v14 pos v15
About this forum
Ajuda

Show or hide a server action button based on a field value on odoo V17 CE

Subscriure's

Get notified when there's activity on this post

This question has been flagged
actiondevelopementvisibilityserver_actions17.0
3 Respostes
2170 Vistes
Avatar
Master

Hi,

How can I show or hide a server action button based on a field value in my model?

For example, if the state field is 'closed', I want the "Open" button to be visible.

And if the state is 'open', I want the "Close" button to be displayed instead.

Is there a way to conditionally show or hide these server actions depending on the record's state?


<record id="mymodel_open_action_server" model="ir.actions.server">

<field name="name">Open mymodel</field>

<field name="model_id" ref="model_mymodel"/>

<field name="binding_model_id" ref="model_mymodel"/>

<field name="binding_view_types">form</field>

<field name="state">code</field>

<field name="code">

action = records.open_action()

</field>

0
Avatar
Descartar
Avatar
Cybrosys Techno Solutions Pvt.Ltd
Best Answer

Hi,

Please refer to the solved forum:

1. https://www.odoo.com/id_ID/forum/help-1/how-to-hide-action-button-based-on-condition-246523

2. https://www.odoo.com/id_ID/forum/help-1/hide-a-menu-inside-action-button-based-on-condition-168198


Hope it helps.

0
Avatar
Descartar
Avatar
slckatav
Best Answer

Hey, could you share your open_action() function as well? Since you’d prefer not to use a button seeing that code will help me suggest the right approach. In general you can add an attrs to your XML to toggle visibility. For example:

attrs="{'invisible': [('state', 'in', ['sale'])]}"

This will hide the element when state is not in the “sale” stage. Of course, you can adjust the domain or use different attribute just a quick example to get you started.

0
Avatar
Descartar
Master
Autor

Hi, you can see my code below for more details:

<!-- Server Action -->
<record id="mymodel_closed_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="state">code</field>
<field name="code">action = records._action_close()</field>
</record>

def _action_close(self):
for rec in self:
if rec.state != "closed":
print("My logic goes here!")
rec.state = "closed"

slckatav

I once hid some action buttons by finding their generated names. Odoo assigns its own names to action buttons like button name="356". In your case, after you add your action button find the name that Odoo generated and then add XML like this:

<xpath expr="//button[@name='466']" position="attributes">
<attribute name="invisible">True</attribute>
</xpath>

I hope this helps!

Avatar
D Enterprise
Best Answer

Hiii,

To show/hide buttons based on the state field in Odoo 17 Community Edition , you must use button elements inside the form view , not ir.actions.server bindings, because ir.actions.server buttons do not support dynamic visibility using attrs .

Here's the correct and clean implementation :

Model: mymodel.py

Define state , action_open , and action_close :

from odoo import models, fields class MyModel (models.Model): _name = 'mymodel' _description = 'My Model' state = fields.Selection([ ( 'open' , 'Open' ), ( 'closed' , 'Closed' ) ], default= 'open' , string= 'State' ) def action_open ( self ): for record in self: record.state = 'open' def action_close ( self ): for record in self: record.state = 'closed'

View: mymodel_form_view.xml

Inherit or define your form view and add two buttons, conditionally shown using attrs .

<record id ="view_form_mymodel" model="ir.ui.view"> <field name = "name" >mymodel.form </field> <field name = "model" >mymodel </field> <field name = "arch" type = "xml"> <form string = "My Model"> <header> <button name="action_open"

​ ​ ​ ​ ​        type="object"

​ ​ ​ ​​        string="Open"

        ​ ​ ​ ​ ​invisible="state != 'closed'"/> <button name="action_close"

​ ​ ​ ​ ​        type="object"

​ ​ ​ ​ ​        string="Close"

​ ​ ​ ​ ​        invisible="state != 'open'"/> </header> <sheet> <group> <field name = "state"/> <!-- other fields --> </group> </sheet> </form > </field > </record>

i hope it is use full

0
Avatar
Descartar
Master
Autor

This works with a normal <button> tag. However, in my case, I need to show or hide my custom choices Close/Open in the default server action, similar to the behavior of the Archive/Unarchive button in

D Enterprise

Use the binding_filter_domain field (Best Practice)
From Odoo 15+, ir.actions.server supports a field called binding_filter_domain, which defines when a server action appears in the action dropdown.

Example: Show “Open” action only if state == 'closed'
<record id="mymodel_open_action_server" model="ir.actions.server">
<field name="name">Open</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'closed')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_open()
</field>
</record>
Example: Show “Close” action only if state == 'open'
<record id="mymodel_close_action_server" model="ir.actions.server">
<field name="name">Close</field>
<field name="model_id" ref="model_mymodel"/>
<field name="binding_model_id" ref="model_mymodel"/>
<field name="binding_view_types">form</field>
<field name="binding_filter_domain">[('state', '=', 'open')]</field>
<field name="state">code</field>
<field name="code">
action = records.action_close()
</field>
</record>
please try this
i hoe it is use full

Enjoying the discussion? Don't just read, join in!

Create an account today to enjoy exclusive features and engage with our awesome community!

Registrar-se
Related Posts Respostes Vistes Activitat
¿how to create planned actions? Solved
action actions server_actions
Avatar
Avatar
2
de març 23
3273
(!!copair~rápido!!) Cómo puedo hablar con una persona real en Copa?
action
Avatar
0
de nov. 25
4
Notification Email Alias – What Emails Are Sent?
action
Avatar
0
de nov. 25
5
odoo 19, Enterprise version odoo on - premise.
action
Avatar
Avatar
2
de nov. 25
125
in POS orderline is it possible to add background color to the discounted product line
17.0
Avatar
Avatar
1
de nov. 25
69
Community
  • Tutorials
  • Documentació
  • Fòrum
Codi obert
  • Descarregar
  • GitHub
  • Runbot
  • Traduccions
Serveis
  • Allotjament a Odoo.sh
  • Suport
  • Actualització
  • Desenvolupaments personalitzats
  • Educació
  • Troba un comptable
  • Troba un partner
  • Converteix-te en partner
Sobre nosaltres
  • La nostra empresa
  • Actius de marca
  • Contacta amb nosaltres
  • Llocs de treball
  • Esdeveniments
  • Pòdcast
  • Blog
  • Clients
  • Informació legal • Privacitat
  • Seguretat
الْعَرَبيّة Català 简体中文 繁體中文 (台灣) Čeština Dansk Nederlands English Suomi Français Deutsch हिंदी Bahasa Indonesia Italiano 日本語 한국어 (KR) Lietuvių kalba Język polski Português (BR) română русский язык Slovenský jazyk slovenščina Español (América Latina) Español ภาษาไทย Türkçe українська Tiếng Việt

Odoo és un conjunt d'aplicacions empresarials de codi obert que cobreix totes les necessitats de la teva empresa: CRM, comerç electrònic, comptabilitat, inventari, punt de venda, gestió de projectes, etc.

La proposta única de valor d'Odoo és ser molt fàcil d'utilitzar i estar totalment integrat, ambdues alhora.

Website made with

Odoo Experience on YouTube

1. Use the live chat to ask your questions.
2. The operator answers within a few minutes.

Live support on Youtube
Watch now