Skip to Content
Menú
This question has been flagged
2 Respostes
4098 Vistes

hi all, iam a newbie to odoo iam trying to call a fuction when a boolean field is clicked 

this is my field 

x = fields.Boolean (string = "SAM", default = False)



this is my function, this work fine without the button click condition but when i tried with this conndition i get no error but its not calling the function ie its not working

  1.     @ api.model

  2.     def x (self, vals):

  3.         rec = super (SaleOrder, self) .create (vals)

  4.         if self.transit_check:

  5.             rec = super (SaleOrder, self) .create (vals)

  6.             product_id = self.env ['product.product']. search ([('default_code', '=', 'XX')])

  7.             if product_id:

  8.                 y = 0

  9.                 for each in rec.order_line:

  10.                     y + = each.y +2

  11.                  self.env ['sale.order.line']. create ({'product_id': product_id.id, 'price_unit': y, 'order_id': rec.id, 'product_uom_qty': 1,})

  12.             return rec

  13.         else:

  14.             return rec


someone help me

Avatar
Descartar
Best Answer

There are lots of ways to do this. Like on save, on button click, or may be onchange. I think you want to do it with onchange of the boolean field.

Try the following code:

@api.onchange('x')
def onchange_x(self):
if self.x:
# Find the product
product_id = self.env ['product.product']. search ([('default_code', '=', 'XX')]).id
# Create a new line with the product
self.order_line += self.env['sale.order.line'].new({'product_id': product_id.id, 'price_unit': 10, 'product_uom_qty': 1,})
return {}



Avatar
Descartar
Best Answer

Hello,

As far as I understand onchange events can only return values, warnings or domains (as a dictionary). 

The create method returns record/records which have been created. 

Thanks, 


EDIT: 

I have read your code wrong, I read @api.model as @api.onchange, the condition is not calling because it looks like there is no @api.onchange but even so you cannot return the records at an onchange event. 

My suggestion would be to override the create method, so change the function eg:

# look in the models.py file to find specific decorates for your Odoo

def create(self,values):

    # call super

    rec = super (SaleOrder, self) .create (vals)

    # if the tickbox is checked

    if x:

        # run your code

    

    return rec


I am unsure how to specifically do it for your Odoo, but this is the idea. Once the user saves the document, if x has been ticked then run your code, otherwise do nothing.


Would this work?


Avatar
Descartar
Autor

so what can i give here create method ?

I don't think what you are trying to do is possible.

What is your end goal and then maybe there would be another way to achieve what you are after?

Thanks,

Autor

when i button is clicked iam trying a add a product in the sale order line automatically , now when i create a sale order it will add without button click condition , but with this condition iam unable to add the product(service kind of thing in the line) but no errors so far.

Autor

i have manually added a product (service) , i need to add that in the order line based on button click

I have amended my answer to try and provide a solution

Autor

no its not working but still thanks for the answer , the function gives me no error but its not doing anything

Related Posts Respostes Vistes Activitat
3
de nov. 23
17173
3
de nov. 24
24638
1
d’abr. 23
5973
2
de des. 22
6651
1
de nov. 22
3861