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

AR303000页面运行时动态设置Attention字段必填规则问题

解决Acumatica中动态设置Contact.Attention字段必填性的问题

首先,咱们来梳理下你当前尝试的两个方法存在的问题,然后给出正确的实现方案:

问题分析

第一个方法的问题

  1. 操作了错误的Cache:你在Customer_RowSelected事件里使用的是Customer的Cache,但Attention是Contact表的字段,必须操作Contact的Cache才能生效。
  2. 逻辑错误:不管CustomerClassID是否等于"02",你都设置了PXPersistingCheck.NullOrBlank,这就导致不管什么情况字段都是必填,完全不符合需求。

第二个方法的问题

PXUIRequired的条件直接引用了Customer.customerClassID,但Contact的CacheAttached上下文里,默认无法直接访问当前关联的Customer记录,条件判断会失效。

正确实现方案

我们需要在Customer的RowSelected事件中,获取关联的Contact Cache,然后根据当前Customer的CustomerClassID,动态设置Attention字段的持久化校验和UI必填状态。

以下是完整的扩展代码:

namespace PX.Objects.AR
{
    public class CustomerMaint_Extension : PXGraphExtension<CustomerMaint>
    {
        protected void Customer_RowSelected(PXCache cache, PXRowSelectedEventArgs e)
        {
            Customer customerRow = e.Row as Customer;
            if (customerRow == null) return;

            // 获取Contact的Cache
            PXCache contactCache = Base.Caches[typeof(Contact)];
            // 获取当前Customer对应的主联系人(如果需要处理所有联系人,可遍历Base.Contact.Select()的结果)
            Contact contactRow = Base.Contact.Current;
            if (contactRow == null) return;

            bool isRequired = customerRow.CustomerClassID == "02";

            // 设置后端持久化校验规则
            if (isRequired)
            {
                PXDefaultAttribute.SetPersistingCheck<Contact.attention>(contactCache, contactRow, PXPersistingCheck.NullOrBlank);
            }
            else
            {
                PXDefaultAttribute.SetPersistingCheck<Contact.attention>(contactCache, contactRow, PXPersistingCheck.Nothing);
            }

            // 设置前端UI必填标记(显示星号)
            PXUIFieldAttribute.SetRequired<Contact.attention>(contactCache, contactRow, isRequired);
            // 刷新字段UI,确保变化立即生效
            contactCache.RaiseFieldUpdated(contactRow, typeof(Contact.attention).Name);
        }
    }
}

关键说明

  1. 使用正确的Cache:通过Base.Caches[typeof(Contact)]获取Contact表的Cache,确保我们操作的是Attention字段所属的数据源。
  2. 关联Contact记录:这里获取的是当前Customer的主联系人(Base.Contact.Current),如果你的场景需要处理该Customer下的所有联系人,可以遍历Base.Contact.Select()返回的所有行,逐个设置属性。
  3. 同步后端和前端设置:同时设置PersistingCheck(后端校验)和SetRequired(前端星号显示),确保运行时的校验和页面显示一致。
  4. 刷新字段UI:调用contactCache.RaiseFieldUpdated触发字段更新事件,确保页面加载或CustomerClassID变化时,字段的必填状态立即刷新。

这样修改后,当页面加载或者CustomerClassID改变时,Attention字段会根据条件自动切换必填/非必填状态,完全满足你的需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 07:58:31