调用Persist()之前保存在数据库中的缓存更改

时间:2018-01-09 14:48:36

标签: acumatica

我在“合同模板”屏幕中创建了一个包含Case类的新网格,该网格更新了特定合同模板的案例类。网格中的每个案例类都有一个复选框,当我选中/取消选中时,会触发RowUpdatedEventHandler并且我正在更新Cache的内容。 我重写了Persist()以保存数据库中缓存的内容。但是在调用Persist()之前,更改会保存在数据库中并清除缓存。请有人帮我这个

protected void CRCaseClass_RowUpdated(PXCache sender, PXRowUpdatedEventArgs 
e)
    {
        CRCaseClass newrow = (CRCaseClass)e.Row;
        CRCaseClass oldrow = (CRCaseClass)e.OldRow;
        ContractTemplate row = contracts.Current;
        CaseContract c = new CaseContract();
        CRCaseClassExt newrow_ext = 
              PXCache<CRCaseClass>.GetExtension<CRCaseClassExt>(newrow);
        CRCaseClassExt oldrow_ext = 
              PXCache<CRCaseClass>.GetExtension<CRCaseClassExt>(oldrow);
        c.CaseClassID = newrow.CaseClassID;
        c.ContractID = row.ContractID;
        c.Active = newrow_ext.Check.Value;
        caseContract.Insert(c);

    }


    [PXOverride]
    public void Persist()
    {

        bool c = caseContract.Cache.IsInsertedUpdatedDeleted;
        CaseContract cc = null;
        IEnumerable cacheRecords = caseContract.Cache.Inserted;
        List<CaseContract> recordsToBePersisted = new List<CaseContract>();

        ContractTemplate row = contracts.Current;
        foreach (CaseContract cr in cacheRecords)
        {
            PXResultset<CaseContract> v = PXSelect<CaseContract, Where<CaseContract.contractID, 
                Equal<Required<ContractTemplate.contractID>>, And<CaseContract.caseClassID,
                Equal<Required<CRCaseClass.caseClassID>>>>>.Select(Base, row.ContractID, cr.CaseClassID);
            if (v.Count != 0 && v.Count == 1)
            {
                cc = v.GetEnumerator().Current;
                cc.Active = cr.Active;

            }
            else if (v.Count == 0)
            {
                cc = new CaseContract();
                cc.CaseClassID = cr.CaseClassID;
                cc.ContractID = cr.ContractID;
                cc.Active = cr.Active;
            }

            else {
                //Error Logic
            }
            recordsToBePersisted.Add(cc);
        }

         //clean all cache
        //insert all values from recordsToBePersisted
        caseContract.Cache.Clear();
        foreach (CaseContract i in recordsToBePersisted) {
            caseContract.Insert(i);
        }
        Base.Persist();

1 个答案:

答案 0 :(得分:1)

Acumatica在BLC扩展中覆盖虚方法的方法与您习惯使用.Net框架略有不同。下面是您的代码的更新版本,它应该解决空缓存的问题。有关此主题的更多详细信息,请参阅the Acumatica Customization Guide

[PXOverride]
public void Persist(Action del)
{
    bool c = caseContract.Cache.IsInsertedUpdatedDeleted;
    CaseContract cc = null;
    IEnumerable cacheRecords = caseContract.Cache.Inserted;
    List<CaseContract> recordsToBePersisted = new List<CaseContract>();

    ContractTemplate row = contracts.Current;
    foreach (CaseContract cr in cacheRecords)
    {
        PXResultset<CaseContract> v = PXSelect<CaseContract, Where<CaseContract.contractID, 
            Equal<Required<ContractTemplate.contractID>>, And<CaseContract.caseClassID,
            Equal<Required<CRCaseClass.caseClassID>>>>>.Select(Base, row.ContractID, cr.CaseClassID);
        if (v.Count != 0 && v.Count == 1)
        {
            cc = v.GetEnumerator().Current;
            cc.Active = cr.Active;
        }
        else if (v.Count == 0)
        {
            cc = new CaseContract();
            cc.CaseClassID = cr.CaseClassID;
            cc.ContractID = cr.ContractID;
            cc.Active = cr.Active;
        }
        else 
        {
            //Error Logic
        }
        recordsToBePersisted.Add(cc);
    }

    //clean all cache
    //insert all values from recordsToBePersisted
    caseContract.Cache.Clear();
    foreach (CaseContract i in recordsToBePersisted) 
    {
        caseContract.Insert(i);
    }
    del();
}