Odoo automated server action to trigger another server action

ε祈祈猫儿з 提交于 2019-12-25 08:41:52

问题


Let's say I set up Server Action A on the stock.inventory model. This action simply logs a value and then calls Sever Action B (which has a database ID of 366). The python code in the action is just:

log('running server action a')
value = {
  "type": "ir.actions.server",
   "id": 366,
}

Then, in Server Action B, which is on the product.product model, the python code is just:

log('running server aciton b')

Now, when I add Server Action A to the "More" menu, and manually trigger it from the browser on a stock.inventory object, both actions successfully run. In other words, I see both 'running server action a' and 'running server action b' in the logs.

Now, I create an Automated Action to trigger Server Action A on Update or Create of a stock.inventory object. After doing this, and updating or creating a stock.inventory object via the UI, I only see 'running server action a' in the logs. In other words, Server Action B never gets triggered like it did when I ran the same experiment manually from the "More" menu.

So, my question is whether or not it is possible to trigger a second server action from the first server action if the first server action is triggered by an automated action.


回答1:


I was able to get this working and the solution is very simple. This seems like a pretty cool way for Odoo Online users to treat server actions as functions that can return values to the calling server action.

Here's an example.

Server Action A

a = env['ir.actions.server'].browse(409)
ctx = dict(env.context or {})
ctx.update({'active_id': 169, 'active_model': 'purchase.order'})
a.with_context(ctx).run()

Server Action B (ID = 409)

raise Warning(record)

When you trigger the first action, you'll get the string purchase.order(169,) as output.

Even cooler, if the second server assigns a value to action, it is returned to the first action. For example:

Server Action A

a = env['ir.actions.server'].browse(409)
ctx = dict(env.context or {})
ctx.update({'active_id': 169, 'active_model': 'purchase.order'})
resp = a.with_context(ctx).run()
raise Warning(resp)

Server Action B (ID = 409)

action = record.id

When you trigger the first server action, you'll see 169 as the response.




回答2:


If you have access to the admin section. You should be able to call the function directly. In odoo8 it looks like this.

Pick your server action

Notice the python code section. You should be able to locate the model you need and execute the function directly.

action = self.env['addon.model'].the_fun()

To execute another action, try the following.

action = self.env['ir.actions.server'].ref('xml_id_of_action')
action.run_action_code_multi() 

Here is the description

run_action_code_multi(self, *args, **kwargs) Override to allow returning response the same way action is already returned by the basic server action behavior. Note that response has priority over action, avoid using both.



来源:https://stackoverflow.com/questions/41265559/odoo-automated-server-action-to-trigger-another-server-action

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!