This question has been flagged
4 Replies
5660 Views

Hello all,


I'm working with Odoo Enterprise v13.  I have many products set up that have the cost computed from the bill of materials via clicking on the "Compute Price from BOM" button on the product page.  This seems to run an action that brings up the update price window.  However, BOM item costs change and it is impractical to go through every BOM containing product and click this button regularly.  


How would I go about having a scheduled action trigger that action on every product that has a BOM?


Thank you in advance for any help!

Avatar
Discard
Best Answer

Yes, you can do it using scheduler. Create a scheduler and add following python code to run your method from scheduler for object "product.product":

bom_products = env['mrp.bom'].search([('product_id', '!=', False)]).mapped('product_id')

# Add your method of product.product
bom_products.my_compute_price_method()


Avatar
Discard

Hi, I got an error like this. PLEASE ADVICE

Error:
Odoo Server Error

Traceback (most recent call last):
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_http.py", line 237, in _dispatch
result = request.dispatch()
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 683, in dispatch
result = self._call_function(**self.params)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 359, in _call_function
return checked_call(self.db, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/service/model.py", line 94, in wrapper
return f(dbname, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 347, in checked_call
result = self.endpoint(*a, **kw)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 912, in __call__
return self.method(*args, **kw)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 531, in response_wrap
response = f(*args, **kw)
File "/home/odoo/src/odoo/14.0/addons/web/controllers/main.py", line 1398, in call_button
action = self._call_kw(model, method, args, kwargs)
File "/home/odoo/src/odoo/14.0/addons/web/controllers/main.py", line 1386, in _call_kw
return call_kw(request.env[model], method, args, kwargs)
File "/home/odoo/src/odoo/14.0/odoo/api.py", line 399, in call_kw
result = _call_kw_multi(method, model, args, kwargs)
File "/home/odoo/src/odoo/14.0/odoo/api.py", line 386, in _call_kw_multi
result = method(recs, *args, **kwargs)
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_cron.py", line 83, in method_direct_trigger
cron.with_user(cron.user_id).with_context(lastcall=cron.lastcall).ir_actions_server_id.run()
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_actions.py", line 632, in run
res = runner(run_self, eval_context=eval_context)
File "/home/odoo/src/odoo/14.0/odoo/addons/base/models/ir_actions.py", line 501, in _run_action_code_multi
safe_eval(self.code.strip(), eval_context, mode="exec", nocopy=True) # nocopy allows to return 'action'
File "/home/odoo/src/odoo/14.0/odoo/tools/safe_eval.py", line 346, in safe_eval
raise ValueError('%s: "%s" while evaluating\n%r' % (ustr(type(e)), ustr(e), expr))
Exception

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 639, in _handle_exception
return super(JsonRequest, self)._handle_exception(exception)
File "/home/odoo/src/odoo/14.0/odoo/http.py", line 315, in _handle_exception
raise exception.with_traceback(None) from new_cause
ValueError: <class 'ValueError'>: "Expected singleton: product.product()" while evaluating
"bom_products = env['mrp.bom'].search([('product_id', '!=', False)]).mapped('product_id')\r\n\r\n# Add your method of product.product\r\nbom_products.button_bom_cost()"

Best Answer

I know this isn't what you asked, but here's a fairly simple way to do it manually:


  • Use the Filter to select all products with a BOM

  • If more than 80 records, enter the total number so all are displayed

  • Select all by ticking in the first column of the header

  • In "Action" select Compute Price from BOM.

Avatar
Discard

Why not an automated action on creation & update, to always have the prices updated? ;-)

Best Answer

You can use this, it works for me odoo v14. Using scheduler from the object "product.template".

# Load all BOM records for all the products
rec_boms = self.env['mrp.bom'].search([('product_tmpl_id', '!=', False)]).mapped('product_tmpl_id.id')
# Load the referencing product of the BOM
for ref_prod in rec_boms:
prod = self.env['product.template'].search([('id', '=', ref_prod )])
#Update Price from BOM method to update the product cost
prod.button_bom_cost()
Avatar
Discard
Best Answer

Hello, does anyone have a working version for Odoo 16 for this?

Avatar
Discard