flyingL123
flyingL123

Reputation: 8076

Odoo automated server action to trigger another server action

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.

Upvotes: 1

Views: 3995

Answers (2)

flyingL123
flyingL123

Reputation: 8076

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.

Upvotes: 2

Phillip Stack
Phillip Stack

Reputation: 3378

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

enter image description here

Pick your server action

enter image description here

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.

Upvotes: 0

Related Questions