当固定资产中的自定义字段“企业帐户位置”更改时,我试图在视图中插入历史记录“ ProspectiveAccountLocationHistory”。因此,每次用户在该字段中进行更改时,都会添加一条新记录。我正在尝试做与固定资产中的位置记录完全相同的操作。因此,我的新DAC与DAC'FALocationHistory'非常相似。
因此,为此,我添加了一个数据库表名称“ ProspectiveAccountLocationHistory”。我在表中有CompanyID,AssetID和RevisionID的组合键。
并基于该数据库表生成DAC。
using System;
using PX.Data;
using PX.Objects.FA;
using PX.Objects.CR;
namespace ProspectBusinessAccountInFixedAsset
{
[Serializable]
public class ProspectiveAccountLocationHistory : IBqlTable
{
#region AssetID
[PXDBInt(IsKey = true)]
[PXUIField(Visible=false, Visibility =PXUIVisibility.Invisible)]
[PXParent(typeof(Select<FixedAsset, Where<FixedAsset.assetID, Equal<Current<ProspectiveAccountLocationHistory.assetID>>>>))]
[PXDBLiteDefault(typeof(FixedAsset.assetID))]
public virtual int? AssetID { get; set; }
public abstract class assetID : PX.Data.BQL.BqlInt.Field<assetID> { }
#endregion
#region RevisionID
[PXDBInt(IsKey = true)]
[PXDefault(0)]
public virtual int? RevisionID { get; set; }
public abstract class revisionID : PX.Data.BQL.BqlInt.Field<revisionID> { }
#endregion
#region AccountID
[PXDBInt()]
[PXUIField(DisplayName = "Account")]
[PXDefault(typeof(Search<BAccount.bAccountID, Where<BAccount.bAccountID,Equal<Current<ProspectiveAccountLocationHistory.accountID>>>>))]
[PXSelector(typeof(BAccount.bAccountID), DescriptionField =typeof(BAccount.acctName))]
public virtual int? AccountID { get; set; }
public abstract class accountID : PX.Data.BQL.BqlInt.Field<accountID> { }
#endregion
#region LocationID
[PXDBInt()]
[PXUIField(DisplayName = "Location")]
[PXDefault(typeof(Search<Location.locationID, Where<Location.locationID, Equal<Current<ProspectiveAccountLocationHistory.locationID>>>>))]
[PXSelector(typeof(Location.locationID), DescriptionField = typeof(Location.locationCD))]
public virtual int? LocationID { get; set; }
public abstract class locationID : PX.Data.BQL.BqlInt.Field<locationID> { }
#endregion
#region Tstamps
[PXDBTimestamp()]
[PXUIField(DisplayName = "Tstamps")]
public virtual byte[] Tstamp { get; set; }
public abstract class tstamp : PX.Data.BQL.BqlByteArray.Field<tstamp> { }
#endregion
#region CreatedByID
[PXDBCreatedByID()]
public virtual Guid? CreatedByID { get; set; }
public abstract class createdByID : PX.Data.BQL.BqlGuid.Field<createdByID> { }
#endregion
#region CreatedByScreenID
[PXDBCreatedByScreenID()]
public virtual string CreatedByScreenID { get; set; }
public abstract class createdByScreenID : PX.Data.BQL.BqlString.Field<createdByScreenID> { }
#endregion
#region CreatedDateTime
[PXDBCreatedDateTime()]
[PXUIField(DisplayName = "Created Date Time")]
public virtual DateTime? CreatedDateTime { get; set; }
public abstract class createdDateTime : PX.Data.BQL.BqlDateTime.Field<createdDateTime> { }
#endregion
#region LastModifiedByID
[PXDBLastModifiedByID()]
public virtual Guid? LastModifiedByID { get; set; }
public abstract class lastModifiedByID : PX.Data.BQL.BqlGuid.Field<lastModifiedByID> { }
#endregion
#region LastModifiedByScreenID
[PXDBLastModifiedByScreenID()]
public virtual string LastModifiedByScreenID { get; set; }
public abstract class lastModifiedByScreenID : PX.Data.BQL.BqlString.Field<lastModifiedByScreenID> { }
#endregion
#region LastModifiedDateTime
[PXDBLastModifiedDateTime()]
[PXUIField(DisplayName = "Last Modified Date Time")]
public virtual DateTime? LastModifiedDateTime { get; set; }
public abstract class lastModifiedDateTime : PX.Data.BQL.BqlDateTime.Field<lastModifiedDateTime> { }
#endregion
}
}
然后我首先使用以下视图为AssetMaint创建了图形扩展。
public PXSelect<ProspectiveAccountLocationHistory, Where<ProspectiveAccountLocationHistory.assetID, Equal<Current<FixedAsset.assetID>>>> ProspectiveAccountLocationHistories;
并添加了一个带有带有DataMember ProspectiveAccountLocationHistories的网格的选项卡。
首先,我尝试覆盖persist方法,因此在下面编写了代码。
[PXOverride]
public void Persist(Action del)
{
using (var scope = new PXTransactionScope())
{
var row = Base.LocationHistory.Current;
FALocationHistory assetLocation = Base.AssetLocation.Current;
var assetLocationExt = row.GetExtension<FALocationHistoryExt>();
var item = new ProspectiveAccountLocationHistory()
{
AssetID = Base.Asset.Current.AssetID,
AccountID = assetLocationExt.UsrLHBusinessAccount,
LocationID = assetLocationExt.UsrLHBusinessAccountLocation,
RevisionID = 1
};
ProspectiveAccountLocationHistories.Cache.Insert(item);
ProspectiveAccountLocationHistories.Cache.Persist(PXDBOperation.Insert);
ProspectiveAccountLocationHistories.Cache.Clear();
scope.Complete();
}
del();
}
但是,我一直收到运行时错误,因此以为我可能在自定义字段更新时尝试将记录保存到View Cache。最终,无论如何执行保存,它将在以后保存在数据库中。因此,我在图形扩展中添加了以下事件。
protected void FALocationHistory_UsrLHBusinessAccountLocation_FieldUpdated(PXCache cache, PXFieldUpdatedEventArgs e)
{
try
{
var row = (FALocationHistory)e.Row;
FALocationHistory assetLocation = Base.AssetLocation.Current;
var assetLocationExt = row.GetExtension<FALocationHistoryExt>();
var item = new ProspectiveAccountLocationHistory()
{
AccountID = assetLocationExt.UsrLHBusinessAccount,
LocationID = assetLocationExt.UsrLHBusinessAccountLocation,
RevisionID = 1
};
var t = (ProspectiveAccountLocationHistory)cache.Graph.Caches[typeof(ProspectiveAccountLocationHistory)].Insert(item);
}
catch (Exception ex)
{
PXTrace.WriteError(ex.Message);
}
}
但是在那种情况下,我也会遇到以下相同的运行时错误。
我也检查了其他图形的源代码,并且似乎可以在Field Updated事件中更新缓存。但是,它不起作用。该视图也尽可能简单。我很难找到我在这里缺少的东西。