Skip to Content
Menu
This question has been flagged

I want to automatically assign a SKU (internal reference code) to products depending on if they have the attribute "Organic". If Variants are created I want them only to be given an SKU if they have the attribute "Organic.



My current automated Action:

Model: Product

Active: True

Trigger: On Creation & Update

Trigger Fields:

Before Update Domain: Match all records

Apply on:     Product Attributes > Values > Value = "Organic

                     Internal Reference is not set

                     Product Category > Name = "Food"

Action To Do : Update the Record

Data to write:

                      Field: Internal Reference (product.product)

                      Evaluation Type : Python expression

                      Value: env['ir.sequence'].next_by_code('organic_sku')



When I create a product and give it many variants, all of the variants are given SKUs even though only one of the variants had organic. 

(example the variants: (product1, organic, blue), (product2, conventional, red) and (product3, conventional, green) will all be given an SKU even though only product1 is organic). 


How can it give each variant an SKU only if that variant is Organic?


Thanks for the Help

James Camfield

Odoo 14.0+e (Enterprise Edition)

Avatar
Discard
Best Answer

I think you need to write a python code in the server action.

Create a server action with Action "To Do='Execute Python Code'" and add the following code:

for prod in records:
for tmp_attr_val in prod.product_template_attribute_value_ids:
if tmp_attr_val.attribute_id.name == 'Organic':
prod.default_code = env['ir.sequence'].next_by_code('organic_sku')





Avatar
Discard
Author

Thanks for responding!

I tried using your code, but it wouldn't let me save and had this error message:

forbidden opcode(s) in "# Available variables:\n# - env: Odoo Environment on which the action is triggered\n# - model: Odoo Model of the record on which the action is triggered; is a void recordset\n# - record: record on which the action is triggered; may be void\n# - records: recordset of all records on which the action is triggered in multi-mode; may be void\n# - time, datetime, dateutil, timezone: useful Python libraries\n# - float_compare: Odoo function to compare floats based on specific precisions\n# - log: log(message, level='info'): logging function to record debug information in ir.logging table\n# - UserError: Warning Exception to use with raise\n# To return an action, assign: action = {...}\n\nfor prod in records:\n for tmp_attr_val in prod.product_template_attribute_value_ids:\n if tmp_attr_val.attribute_id.name == 'Organic':\n #prod.write({'default_code' : env['ir.sequence'].next_by_code('organic_sku') })\n prod.default_code = env['ir.sequence'].next_by_code('organic_sku')": STORE_ATTR

I think it didnt allows the = on the last line

So I replaced it with

prod.write({'default_code' : env['ir.sequence'].next_by_code('organic_sku') })

Unfourtunately it gave this error when the model was 'Product Template':

Error:

Odoo Server Error

Traceback (most recent call last):

File "/home/odoo/src/odoo/odoo/addons/base/models/ir_http.py", line 237, in _dispatch

result = request.dispatch()

File "/home/odoo/src/odoo/odoo/http.py", line 683, in dispatch

result = self._call_function(**self.params)

File "/home/odoo/src/odoo/odoo/http.py", line 359, in _call_function

return checked_call(self.db, *args, **kwargs)

File "/home/odoo/src/odoo/odoo/service/model.py", line 94, in wrapper

return f(dbname, *args, **kwargs)

File "/home/odoo/src/odoo/odoo/http.py", line 347, in checked_call

result = self.endpoint(*a, **kw)

File "/home/odoo/src/odoo/odoo/http.py", line 912, in __call__

return self.method(*args, **kw)

File "/home/odoo/src/odoo/odoo/http.py", line 531, in response_wrap

response = f(*args, **kw)

File "/home/odoo/src/odoo/addons/web/controllers/main.py", line 1745, in run

result = action.run()

File "/home/odoo/src/odoo/odoo/addons/base/models/ir_actions.py", line 632, in run

res = runner(run_self, eval_context=eval_context)

File "/home/odoo/src/odoo/addons/website/models/ir_actions.py", line 61, in _run_action_code_multi

res = super(ServerAction, self)._run_action_code_multi(eval_context)

File "/home/odoo/src/odoo/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/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/odoo/http.py", line 639, in _handle_exception

return super(JsonRequest, self)._handle_exception(exception)

File "/home/odoo/src/odoo/odoo/http.py", line 315, in _handle_exception

raise exception.with_traceback(None) from new_cause

ValueError: <class 'TypeError'>: "'NoneType' object is not iterable" while evaluating

"# Available variables:\n# - env: Odoo Environment on which the action is triggered\n# - model: Odoo Model of the record on which the action is triggered; is a void recordset\n# - record: record on which the action is triggered; may be void\n# - records: recordset of all records on which the action is triggered in multi-mode; may be void\n# - time, datetime, dateutil, timezone: useful Python libraries\n# - float_compare: Odoo function to compare floats based on specific precisions\n# - log: log(message, level='info'): logging function to record debug information in ir.logging table\n# - UserError: Warning Exception to use with raise\n# To return an action, assign: action = {...}\n\nfor prod in records:\n for tmp_attr_val in prod.product_template_attribute_value_ids:\n if tmp_attr_val.attribute_id.name == 'Organic':\n prod.write({'default_code' : env['ir.sequence'].next_by_code('organic_sku') })\n #prod.default_code = env['ir.sequence'].next_by_code('organic_sku')"

When I used 'Product' as the model nothing happened

What is the soloution?

Thanks for your help!!!

Related Posts Replies Views Activity
0
Sep 23
2046
1
Feb 24
582
2
Jan 24
599
0
Oct 21
1401
0
May 21
1148