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

如何在SOOrder_Hold_FieldUpdated事件中遍历销售订单行及自定义扩展字段?

在SOOrder_Hold_FieldUpdated事件中遍历销售订单行及扩展字段的实现方案

我来帮你梳理一下在这个事件里遍历销售订单行和扩展字段的具体实现方法,完全贴合你的业务需求:

核心思路

当Hold字段更新时(也就是用户试图打开或锁定订单时),先获取当前订单对应的所有销售订单行,逐个检查自定义字段是否填写完整,同时完成与其他表的校验逻辑,只有所有行都满足条件,才允许将订单标记为已打开状态。

完整实现代码

下面是整合后的事件代码,你可以直接根据自己的实际字段和校验需求调整:

protected void SOOrder_Hold_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e, PXFieldUpdated InvokeBaseHandler)
{
    // 先调用基类处理逻辑
    if (InvokeBaseHandler != null)
        InvokeBaseHandler(cache, e);

    var currentOrder = (SOOrder)e.Row;
    if (currentOrder == null) return;

    // 1. 查询当前订单下的所有销售订单行
    var orderLines = PXSelect<SOOrderLine, 
        Where<SOOrderLine.orderType, Equal<Current<SOOrder.orderType>>, 
            And<SOOrderLine.orderNbr, Equal<Current<SOOrder.orderNbr>>>>>.Select(Base);

    bool isAllLinesValid = true;

    // 2. 遍历每一行,检查自定义字段和关联表校验
    foreach (SOOrderLine line in orderLines)
    {
        // 获取订单行的扩展字段(替换成你实际的扩展类和字段名)
        SOOrderLineExt lineExtension = line.GetExtension<SOOrderLineExt>();
        string requiredCustomField = lineExtension.UsrRequiredCustomField?.Trim();

        // 检查自定义字段是否已填写
        if (string.IsNullOrEmpty(requiredCustomField))
        {
            isAllLinesValid = false;
            // 在界面上显示该行的错误提示
            cache.RaiseExceptionHandling<SOOrder.hold>(currentOrder, currentOrder.Hold,
                new PXSetPropertyException("第 {0} 行的必填自定义字段未填写,请完善后再尝试打开订单", line.LineNbr));
            break; // 遇到第一个不满足的行就停止检查,也可以去掉break收集所有错误
        }

        // 3. 添加与其他表的校验逻辑(示例:检查关联的库存商品是否符合要求)
        InventoryItem inventoryItem = PXSelect<InventoryItem,
            Where<InventoryItem.inventoryID, Equal<Required<SOOrderLine.inventoryID>>>>.Select(Base, line.InventoryID);
        if (inventoryItem == null || !inventoryItem.StkItem.HasValue || !inventoryItem.StkItem.Value)
        {
            isAllLinesValid = false;
            cache.RaiseExceptionHandling<SOOrder.hold>(currentOrder, currentOrder.Hold,
                new PXSetPropertyException("第 {0} 行的商品不是库存商品,不符合要求", line.LineNbr));
            break;
        }
    }

    // 4. 控制订单状态:如果用户试图取消Hold(打开订单)但校验不通过,恢复Hold状态
    if (!currentOrder.Hold.HasValue || !currentOrder.Hold.Value)
    {
        if (!isAllLinesValid)
        {
            cache.SetValue<SOOrder.hold>(currentOrder, true);
            throw new PXException("部分订单行未满足要求,无法将订单标记为已打开");
        }
    }
}

关键注意事项

  • 替换扩展类和字段名:把代码中的SOOrderLineExt和UsrRequiredCustomField替换成你实际创建的订单行扩展类和自定义字段名称
  • 自定义校验逻辑:你可以根据业务需求修改其他表的校验部分,比如关联客户表、供应商表或者其他自定义表的检查
  • 错误提示优化:如果想要显示所有错误而不是只显示第一个,可以去掉break,用列表收集所有错误信息后统一抛出
  • 性能考虑:如果订单行数量较多,可以考虑使用更高效的查询方式,比如PXSelectJoin来关联查询,减少数据库请求次数

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 08:20:52