Skip to Content
Menu
This question has been flagged
2 Replies
3410 Views

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
Discard
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
Discard
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
Discard
Author

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,

Author

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.

Author

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

Author

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

Related Posts Replies Views Activity
3
Nov 23
15311
3
Nov 24
21068
1
Apr 23
4564
2
Dec 22
5497
1
Nov 22
2661