+3 votes
111 views

In Odoo 8, I was able to define a server action using XML such as:

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
  <data>
    <record id="action" model="ir.actions.server">
      <field name="name">My Action</field>
      <field name="model_id" ref="model_module_model"/>
      <field name="code">self.action(cr, uid, context=context)</field>
    </record>
  </data>
</odoo>

This would execute my module.model.action() method.

In Odoo 10, this code throws an exception:

ERROR:odoo.http:Exception during JSON request handling.
Traceback (most recent call last):
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 638, in _handle_exception
    return super(JsonRequest, self)._handle_exception(exception)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 675, in dispatch
    result = self._call_function(**self.params)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 331, in _call_function
    return checked_call(self.db, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/service/model.py", line 119, in wrapper
    return f(dbname, *args, **kwargs)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 324, in checked_call
    result = self.endpoint(*a, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 933, in __call__
    return self.method(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/http.py", line 504, in response_wrap
    response = f(*args, **kw)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/web/controllers/main.py", line 1129, in run
    result = request.env['ir.actions.server'].browse([action_id]).run()
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 964, in run
    res = func(action, eval_context=eval_context)
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/addons/base/ir/ir_actions.py", line 793, in run_action_code_multi
    safe_eval(action.code.strip(), eval_context, mode="exec", nocopy=True)  # nocopy allows to return 'action'
File "/usr/share/odoo/lib/python2.7/site-packages/odoo/tools/safe_eval.py", line 301, in safe_eval
    return unsafe_eval(c, globals_dict, locals_dict)
File "", line 1, in <module>
ValueError: <type 'exceptions.NameError'>: "name 'self' is not defined" while evaluating
u'self.action(cr, uid, context=context)'

I'm not seeing anything interesting in the Odoo documentation about server actions at https://www.odoo.com/documentation/10.0/reference/actions.html#code

And the docs explicitly state that self is part of the evaluation context for server actions, see https://www.odoo.com/documentation/10.0/reference/actions.html#reference-actions-server-context

How does one create server actions in Odoo 10?

closed with the note: problem fixed
in Back-end by (10.5k points)
closed by | 111 views

2 Answers

+3 votes

There is no need to self object while declare server actions in Odoo 10. We can directly access model/object with env['model.name']

Try with following code:

<?xml version="1.0" encoding="utf-8" ?>
<odoo>
  <data>
    <record id="action" model="ir.actions.server">
      <field name="name">My Action</field>
      <field name="model_id" ref="model_module_model"/>
      <field name="code">
        if context.get('active_model') == 'your.module.model' and context.get('active_ids'):
                action = env['module.model'].browse(context['active_ids']).action()
    </record>
  </data>
</odoo>
by (4.6k points)

Hi, the issue concerns odoo 8 and not odoo 10

Probably You did not read the question well

he say How does one create server actions in Odoo 10?

+1 vote
Thanks I will try that
by (10.5k points)

Have you tried it? I am still waiting for the result

205 questions
180 answers
37 comments
220,612 users