我们说我在stock.inventory
模型上设置了服务器操作A.此操作只记录一个值,然后调用Sever Action B(其数据库ID为366)。动作中的python代码只是:
log('running server action a')
value = {
"type": "ir.actions.server",
"id": 366,
}
然后,在product.product
模型上的服务器操作B中,python代码只是:
log('running server aciton b')
现在,当我将服务器操作A添加到"更多"菜单,并从stock.inventory
对象上的浏览器手动触发它,两个操作都成功运行。换句话说,我发现两者都在运行服务器操作a'并且'运行服务器操作b'在日志中。
现在,我创建了一个自动操作,以便在更新或创建stock.inventory
对象时触发服务器操作A.执行此操作,并通过用户界面更新或创建stock.inventory
对象后,我只看到“正在运行服务器操作a'在日志中。换句话说,服务器操作B永远不会像我从"更多"手动运行相同的实验时那样被触发。菜单。
所以,我的问题是,如果第一个服务器操作是由自动操作触发的,是否可以从第一个服务器操作触发第二个服务器操作。
答案 0 :(得分:1)
我能够使这个工作,解决方案非常简单。对于Odoo Online用户来说,这似乎是一种非常酷的方式,可以将服务器操作视为可以将值返回给调用服务器操作的函数。
这是一个例子。
服务器操作A
[WebMethod]
public string getUserbyUsername(string uname, string passw)
{
string cs = "Data Source =.; Initial Catalog = UsersDB; Integrated Security = True";
using (SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spGetUserByUsername", con);
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter parameter = new SqlParameter(@"Username", uname);
SqlParameter parameter2 = new SqlParameter(@"Password", passw);
cmd.Parameters.Add(parameter);
cmd.Parameters.Add(parameter2);
User user = new User();
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
// user.Username = reader["Username"].ToString();
// user.Password = reader["Password"].ToString();
user.IsExisting = reader["IsExisting"].ToString();
user.UserID = reader["UserID"].ToString();
}
con.Close();
return new JavaScriptSerializer().Serialize(user);
}
}
服务器操作B(ID = 409)
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()
当您触发第一个操作时,您将获得字符串raise Warning(record)
作为输出。
更酷,如果第二台服务器为purchase.order(169,)
分配值,则返回第一个操作。例如:
服务器操作A
action
服务器操作B(ID = 409)
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)
当您触发第一个服务器操作时,您将看到169作为响应。
答案 1 :(得分:0)
如果您有权访问管理部分。您应该能够直接调用该函数。在odoo8中它看起来像这样。
选择您的服务器操作
注意python代码部分。您应该能够找到所需的模型并直接执行该功能。
action = self.env['addon.model'].the_fun()
要执行其他操作,请尝试以下操作。
action = self.env['ir.actions.server'].ref('xml_id_of_action')
action.run_action_code_multi()
以下是说明
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.