You need to enable JavaScript to run this app. Acumatica FS300200界面PXAction无法保存Address扩展缓存数据
优惠活动
大模型
产品
解决方案
定价
更多

Acumatica FS300200界面PXAction无法保存Address扩展缓存数据

根因定位

字段无法持久化是两个核心问题叠加导致的:

  1. 在DoWork方法里用独立PXSelect查出来的Address实体,没有被Appointments界面Graph的Address缓存纳入跟踪范围,修改的是一份从数据库临时查出来的内存副本,不是界面正在缓存、会参与事务提交的那份实体
  2. 同时混用了SetValueExt和手动直接给扩展类属性赋值的操作,打乱了PXCache的变更检测标记,就算调用了Update,缓存也识别不到这个实体发生了变更,自然不会生成对应的数据库更新语句
正确实现步骤

1. 不要自定义独立的Address缓存上下文

把之前在Graph扩展里写的独立Addresslist PXSelect删掉,FSAppointmentEntry本身自带完整的BAccount、Location、Address缓存链路,自己新建的PXSelect虽然能查到同一张表的数据,但和Base Graph正在跟踪的缓存是两个完全独立的内存对象,修改了也不会被提交。

2. 正确获取被缓存跟踪的Address实体

查地址的时候优先从当前Graph的Address缓存里匹配,缓存里找不到的再从数据库查询,查到后显式把实体挂载到缓存里跟踪,不要直接对无绑定查询出来的脱管实体做修改。

3. 正确的扩展字段赋值姿势

扩展字段写值统一用PXCache的SetValueExt方法,这个方法会自动触发字段的事件链路、校验逻辑、脏标记,不要手动直接拿Extension类的实例给属性赋值——手动赋值不会触发缓存的变更检测,就算Update了也不会被识别为需要持久化的变更。

修正后的代码参考

public PXAction<PX.Objects.FS.FSAppointment> DoWork;
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "DoWork")]
protected void doWork()
{
    try
    {
        FSServiceOrder curFSServiceOrder = Base.ServiceOrderRelated.Current;
        if (curFSServiceOrder?.CustomerID == null) return;

        // 获取当前服务单对应的客户
        BAccount curBAccount = PXSelect<BAccount,
            Where<BAccount.bAccountID, Equal<Required<FSServiceOrder.customerID>>>>
            .SelectSingleBound(Base, null, curFSServiceOrder.CustomerID);
        
        // 获取客户对应的定位信息
        Location curLocation = PXSelect<Location,
            Where<Location.bAccountID, Equal<Required<Location.bAccountID>>,
                And<Location.locationID, Equal<Required<Location.locationID>>>>>
            .SelectSingleBound(Base, null, curBAccount.BAccountID, curFSServiceOrder.LocationID);
        
        // 直接拿Base Graph自带的Address缓存
        PXCache addressCache = Base.Caches[typeof(Address)];
        // 先从已缓存的实体里匹配目标地址
        Address curAddress = addressCache.Cached
            .Cast<Address>()
            .FirstOrDefault(a => a.AddressID == curLocation.DefAddressID && a.BAccountID == curBAccount.BAccountID);
        
        // 缓存里没找到的话,再从数据库查,并且显式挂载到缓存中
        if (curAddress == null)
        {
            curAddress = PXSelect<Address,
                Where<Address.addressID, Equal<Required<Address.addressID>>,
                    And<Address.bAccountID, Equal<Required<Address.bAccountID>>>>>
                .SelectSingleBound(Base, null, curLocation.DefAddressID, curBAccount.BAccountID);
            curAddress = (Address)addressCache.Insert(curAddress);
        }

        // 实际业务中计算得到的经纬度
        string DeliveryAddressLongitude = "fakeLong";
        string DeliveryAddressLattitude = "fakeLant"; 

        // 用SetValueExt给扩展字段赋值,自动触发缓存事件和脏标记
        addressCache.SetValueExt<AddressExt.usrDeliveryAddressLongitude>(curAddress, DeliveryAddressLongitude);
        addressCache.SetValueExt<AddressExt.usrDeliveryAddressLattitude>(curAddress, DeliveryAddressLattitude);

        // 显式触发Update,确认实体加入待提交队列
        addressCache.Update(curAddress);
    }
    catch (Exception ex)
    {
        PXTrace.WriteError(ex);
    }
}
容易踩的额外坑
  • 写扩展字段的时候不要混用赋值方式:手动拿PXCache<Address>.GetExtension读字段是没问题的,但写字段必须走SetValueExt,不然缓存识别不到变更
  • 不需要手动调用Base.Persist(),按钮上的CommitChanges = true属性已经会在操作执行完后自动提交所有被标记为脏的缓存实体
  • 确认你的AddressExt扩展类是符合Acumatica规范的PXCacheExtension<Address>子类,扩展字段的BqlField标记正确,不然就算赋值成功,也不会映射到数据库对应的自定义字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.26 20:06:07