更新自定义字段将结束为无限循环

时间:2017-09-11 14:12:05

标签: acumatica

我在AR发票和备忘录(屏幕ID AR301000)中有相应AP参考的自定义字段。 NBR。在类似的经理中,AP账单和调整中的另一个自定义字段(屏幕ID AP301000)用于相应的AR Ref。 NBR。

我正在尝试更新AP Ref。 NBR。在用户更新AR Ref时在AR屏幕上显示。 NBR。在AP屏幕上。

例如 -

我在AR屏幕发票0001,我正在更新AP Ref。 NBR。到abc01。系统将自动使用相应的AR Ref更新AP Bill abc01。 NBR。用0001。

我已经编写了下面的代码来实现这一点,但它会遇到无限循环,因为它试图更新其他屏幕中的相应字段。如果我遗漏任何东西或者有更好的方法,请告诉我。

在AR图表扩展



public class ARInvoiceEntryExtension : PXGraphExtension<ARInvoiceEntry>
{
	protected virtual void ARInvoice_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
	{
		var row = (ARInvoice)e.Row;
		if (row != null && sender.IsDirty)
		{
			ARRegisterExtension ext = PXCache<ARRegister>.GetExtension<ARRegisterExtension>(row);

			if (ext != null && !string.IsNullOrEmpty(ext.UsrAPRefNbr))
			{
				APInvoiceEntry graph = PXGraph.CreateInstance<APInvoiceEntry>();

				APInvoice apRow = PXSelect<APInvoice,
					Where<APInvoice.refNbr, Equal<Required<APInvoice.refNbr>>>>.Select(graph, ext.UsrAPRefNbr);

				if (apRow != null)
				{
					APRegisterExtension ext1 = PXCache<APRegister>.GetExtension<APRegisterExtension>(apRow);
					if (ext1 != null && string.IsNullOrEmpty(ext1.UsrARRefNbr))        //Update only if it is empty
					{
						ext1.UsrARRefNbr = row.RefNbr;

						graph.Document.Current = apRow;

						graph.Caches[typeof(APRegister)].SetValue<APRegisterExtension.usrARRefNbr>(apRow, row.RefNbr);
						graph.Caches[typeof(APRegister)].Update(apRow);
						graph.Actions.PressSave();
					}
				}
			}
		}
	}
}
&#13;
&#13;
&#13;

在AP图表扩展

&#13;
&#13;
public class APInvoiceEntryExtension : PXGraphExtension<APInvoiceEntry>
{
	protected virtual void APInvoice_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e)
	{
		var row = (APInvoice)e.Row;
		if (row != null && sender.IsDirty)
		{
			APRegisterExtension ext = PXCache<APRegister>.GetExtension<APRegisterExtension>(row);

			if (ext != null && !string.IsNullOrEmpty(ext.UsrARRefNbr))
			{
				ARInvoiceEntry graph = PXGraph.CreateInstance<ARInvoiceEntry>();

				ARInvoice arRow = PXSelect<ARInvoice,
					Where<ARInvoice.refNbr, Equal<Required<ARInvoice.refNbr>>>>.Select(graph, ext.UsrARRefNbr);

				if (arRow != null)
				{
					ARRegisterExtension ext1 = PXCache<ARRegister>.GetExtension<ARRegisterExtension>(arRow);
					if (ext1 != null && string.IsNullOrEmpty(ext1.UsrAPRefNbr))        //Update only if it is empty
					{
						ext1.UsrAPRefNbr = row.RefNbr;

						graph.Document.Current = arRow;

						graph.Caches[typeof(ARRegister)].SetValue<ARRegisterExtension.usrAPRefNbr>(arRow, row.RefNbr);
						graph.Caches[typeof(ARRegister)].Update(arRow);
						graph.Actions.PressSave();
					}
				}
			}
		}
	}
}
&#13;
&#13;
&#13;

AR缓存扩展

&#13;
&#13;
public class ARRegisterExtension : PXCacheExtension<ARRegister>
{
	public abstract class usrAPRefNbr : PX.Data.IBqlField
	{
	}

	protected string _usrAPRefNbr;

	[PXDBString(15)]
	[PXUIField(DisplayName = "AP Ref Nbr.", Visibility = PXUIVisibility.SelectorVisible)]

	[APInvoiceType.RefNbr(typeof(Search3<PX.Objects.AP.Standalone.APRegisterAlias.refNbr,
	InnerJoinSingleTable<APInvoice, On<APInvoice.docType, Equal<PX.Objects.AP.Standalone.APRegisterAlias.docType>,
		And<APInvoice.refNbr, Equal<PX.Objects.AP.Standalone.APRegisterAlias.refNbr>>>,
	InnerJoinSingleTable<Vendor, On<PX.Objects.AP.Standalone.APRegisterAlias.vendorID, Equal<Vendor.bAccountID>>>>,
	OrderBy<Desc<APRegister.refNbr>>>))]
	public virtual string UsrAPRefNbr
	{
		get; set;
	}
}
&#13;
&#13;
&#13;

AP缓存扩展

&#13;
&#13;
public class APRegisterExtension : PXCacheExtension<APRegister>
{
	public abstract class usrARRefNbr : PX.Data.IBqlField
	{
	}

	protected string _usrARRefNbr;

	[PXDBString(15)]
	[PXUIField(DisplayName = "AR Ref Nbr.", Visibility = PXUIVisibility.SelectorVisible)]

	[ARInvoiceType.RefNbr(typeof(Search3<PX.Objects.AR.Standalone.ARRegisterAlias.refNbr,
	InnerJoinSingleTable<ARInvoice, On<ARInvoice.docType, Equal<PX.Objects.AR.Standalone.ARRegisterAlias.docType>,
		And<ARInvoice.refNbr, Equal<PX.Objects.AR.Standalone.ARRegisterAlias.refNbr>>>,
	InnerJoinSingleTable<Customer, On<PX.Objects.AR.Standalone.ARRegisterAlias.customerID, Equal<Customer.bAccountID>>>>,
	OrderBy<Desc<ARRegister.refNbr>>>))]
	public virtual string UsrARRefNbr
	{
		get; set;
	}
}
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

保存时,APInvoice_RowUpdated修改ARInvoice,后者又修改APInvoice,触发APInvoice_RowUpdated,产生无限循环的事件调用。更新APInvoice的ARInvoice_RowUpdated中的反转将导致类似的无限循环。

为了摆脱这种情况,您可以在实例化图形后在运行时删除图形事件处理程序。首先使您的事件处理程序访问修饰符公开,以便您可以引用它们:

public virtual void APInvoice_RowUpdated(PXCache sender, PXRowUpdatedEventArgs e) 

创建图形后,获取扩展并删除导致无限循环的处理程序:

APInvoiceEntry graph = PXGraph.CreateInstance<APInvoiceEntry>();
APInvoiceEntryExtension graphExt = graph.GetExtension<APInvoiceEntryExtension>();
graphExt.RowUpdated.RemoveHandler<APInvoice>(graphExt.APInvoice_RowUpdated);

必须对ARInvoice进行相同的修改,因为无限循环是双向的,从AP到AR,从AR到AP。