1

We have a case where we want to conditionally auto number shipments based on if they were created via the API or via the UI. I've got that part figured out, but I can't figure out the best way to set the ShipmentNbr. I know that I can update the ShipmentNbr in the RowPersisting event before it is saved to the database (like below), but that doesn't update the ShipmentNbr in the other related tables (SOOrderShipment, SOShipLine, SOShipLineSplit, etc). I also tried looping through the related records and updating them individually, but that didn't work either. I could do it in the RowInserting event, but that causes issues when dealing with concurrency. Is there any way I can update the ShipmentNbr for the shipment and its related records?

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
        }
}
Slottaje
  • 65
  • 5

1 Answers1

0

Well, I figured it out. I had some corrupt data which prevented the RowPersisting event from working correctly. Credit to Sergey here https://asiablog.acumatica.com/2018/05/auto-numbering-customization.html

Here's how I did it if anyone else needs to do the same:

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
    }
}
Slottaje
  • 65
  • 5