You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Odoo项目模块:如何避免手动创建项目时触发自动化动作?

解决Odoo自动化动作误触发问题

你需要给自动化动作加个判断逻辑,只有当项目关联了销售订单(也就是record.sale_order_id不为空)的时候,才执行生成名称的代码,这样手动创建项目时就不会触发了。

修正后的完整代码

# 仅在项目关联销售订单时执行名称生成逻辑
if record.sale_order_id:
    name = record.sale_order_id.partner_id.name
    # 修复原代码语法错误:""join → "".join,也可简化为shortage = name.split()[0]
    shortage = "".join(name.split()[0])
    sum_of_qty = 0

    fc_numbers = []
    # 优化遍历路径:直接从销售订单取订单行,无需通过sale_line_id中转
    for line_id in record.sale_order_id.order_line:
        if line_id.x_studio_product_type == "service" and line_id.x_studio_create_on_order == "task_in_project":
            sum_of_qty += int(line_id.product_uom_qty)
            if not line_id.x_studio_fc:
                fc_numbers.append("TBD")
            elif line_id.x_studio_fc not in fc_numbers:
                fc_numbers.append(line_id.x_studio_fc)
    # 拼接最终项目名称
    record["name"] = f"{' / '.join(fc_numbers)} - {record.sale_order_id.name} - {shortage} - {sum_of_qty} MT"

额外优化建议

  1. 简化代码细节:name.split()[0]本身就是单个字符串,"".join()可以直接省略,写成shortage = name.split()[0]更简洁。
  2. 双重保险:设置动作触发条件:除了代码内的判断,还可以在自动化动作的触发条件里添加规则:sale_order_id 不为空,从触发层面直接过滤手动创建的情况,更稳妥。
  3. 避免潜在报错:原代码通过record.sale_line_id.order_id取销售订单,若项目未关联任何销售订单行,会导致报错,直接用record.sale_order_id更直接可靠。

内容的提问来源于stack exchange,提问作者kayalotta

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 11:00:49