我们有一个案例,我们希望根据货件是通过 API 还是通过 UI 创建的,有条件地自动编号。我已经弄清楚了这一部分,但我无法找出设置 ShipmentNbr 的最佳方法。我知道我可以在将 RowPersisting 事件中的 ShipmentNbr 保存到数据库之前对其进行更新(如下所示),但这不会更新其他相关表(SOOrderShipment、SOShipLine、SOShipLineSplit 等)中的 ShipmentNbr。我还尝试循环遍历相关记录并单独更新它们,但这也不起作用。我可以在 RowInserting 事件中执行此操作,但这会在处理并发时引起问题。有什么方法可以更新货件的 ShipmentNbr 及其相关记录吗?
public void SOShipment_RowPersisting(PXCache sender, PXRowPersistingEventArgs e)
{
SOShipment row = (SOShipment)e.Row;
if (row.ShipmentNbr.Trim() == "<NEW>")
{
row.ShipmentNbr = AutoNumberAttribute.GetNextNumber(sender, row, "SOSHIPMENT", DateTime.Now);
// this works, but doesn't update related records
}
}
好吧,我想通了。我有一些损坏的数据,导致 RowPersisting 事件无法正常工作。感谢 Sergey https://asiablog.acumatica.com/2018/05/auto-numbering-customization.html
如果其他人需要这样做,我就是这样做的:
namespace PX.Objects.SO
{
public class SOShipmentEntry_Extension : PXGraphExtension<SOShipmentEntry>
{
#region Event Handlers
[PXDBString(15, IsKey = true, IsUnicode = true, InputMask = ">CCCCCCCCCCCCCCC")]
[PXDefault()]
[PXUIField(DisplayName = "Shipment Nbr.", Visibility = PXUIVisibility.SelectorVisible, IsReadOnly = true)]
// [AutoNumber(typeof(SOSetup.shipmentNumberingID), typeof(SOShipment.shipDate))]
[PXSelector(typeof(Search2<SOShipment.shipmentNbr,
InnerJoin<INSite, On<SOShipment.FK.Site>,
LeftJoinSingleTable<Customer, On<SOShipment.customerID, Equal<Customer.bAccountID>>>>,
Where2<Match<INSite, Current<AccessInfo.userName>>,
And<Where<Customer.bAccountID, IsNull, Or<Match<Customer, Current<AccessInfo.userName>>>>>>,
OrderBy<Desc<SOShipment.shipmentNbr>>>))]
[PX.Data.EP.PXFieldDescription]
protected void SOShipment_ShipmentNbr_CacheAttached(PXCache sender)
{
}
protected virtual void SOShipment_ShipmentNbr_FieldDefaulting(PXCache sender, PXFieldDefaultingEventArgs e)
{
SOShipment shipment = e.Row as SOShipment;
if (shipment.ShipmentNbr == null)
{
e.NewValue = "<NEW>";
}
}
protected void SOShipment_RowPersisting(PXCache cache, PXRowPersistingEventArgs e, PXRowPersisting InvokeBaseHandler)
{
SOShipment row = (SOShipment)e.Row;
if (InvokeBaseHandler != null)
InvokeBaseHandler(cache, e);
if ((e.Operation & PXDBOperation.Command) != PXDBOperation.Insert)
{
return;
}
var httpContext = HttpContext.Current;
// don't auto number if shipment was created via api
if ( !(httpContext != null && httpContext.Items != null && httpContext.Items.Contains("ApiContextId")) )
{
var num = AutoNumberAttribute.GetNextNumber(cache, row, "SOSHIPMENT", DateTime.Now);
cache.SetValue(e.Row, "ShipmentNbr", num);
}
}
#endregion
}
}
对于客户,我们可以使用:
protected void Customer_AcctCD_FieldDefaulting(PXCache cache, PXFieldDefaultingEventArgs e)
{
var row = (Customer)e.Row;
if (row==null) return;
e.NewValue = "<NOUVEAU>";
}
protected void Customer_RowPersisting(PXCache cache, PXRowPersistingEventArgs e)
{
var row = (Customer)e.Row;
if (row==null) return;
if ((e.Operation & PXDBOperation.Command) != PXDBOperation.Insert) {return;}
string codeclient=row.AcctCD;
if (codeclient.Contains("<NOUVEAU>")==true)
{
var num = AutoNumberAttribute.GetNextNumber(cache, row, "CUSTOMER", DateTime.Now);
cache.SetValue<Customer.acctCD>(row,num);
}
}