I am learning Odoo 11 and want to code a button which will call a python method through a Server action.
Here's my button code:
<button name="enviarfactura" type="action" string="Homologar" attrs="{'invisible':['|',('sent','=',True), ('state', 'not in', ('open','paid'))]}"/>
Here's my action.server code
<record model="ir.actions.server" id="x_nc_act_serv_fact">
<field name="name">enviarfactura</field>
<field name="model_id" ref="model_account_invoice"/>
<field name="sequence">1</field>
<field name="type">ir.actions.server</field>
<field name="state">code</field>
<field name="code">
if records:
action = records.x_nc_met_fac()
</field>
</record>
And finally here's my python method.
@api.multi
def x_nc_met_fac(self):
for rec in self:
self.x_nc_fld_fact = True
self.x_nc_fld_det_fact = 'my custom text'
Now the logic for this is that, the button will call the server.action which will call the method on my python class. The method will set new values for extended fields on the model account.invoice In theory this should work, but it doesn't.
Discarding possible mistakes:
-It's might not be the python indentation. The python community on discord help me with it :P
-It's not the server.action structure since when I upload my module containing this code it creates the server.action without any problem.
-When I click on the button, nothing happens.So i tried changing the type of the button to "type=object"
and tell it the name of my method it gives the error. Account.invoice has no attribute "x_nc_met_fac"
Any help is appreciated. Thanks in advance.
The name
of the button has to be the action ID. So two thinks have to be done:
Your action should be defined before the button. If both are in the same xml, it's easy, if not check the order in your module manifest. XMLs will be loaded in that order!
Change button name
to an "external ID substitution"
<button name="%(x_nc_act_serv_fact)d" type="action" string="Homologar"
attrs="{'invisible':['|',('sent','=',True), ('state', 'not in', ('open','paid'))]}"/>