Skip to main content

Hello community.  In the code below I am doing a few things, creating a sales order of the transfer type from shipments of the receipt type. I am saving the shipment number to the transfer order and the newly created sales order number to the shipment. It is doing exactly what I want minus saving the new SO # to the shipment record. I am doing this in an enclosed “using (var ts = new PXTransactionScope())” and was wondering if its possible I am not able to update multiple graph instances at once? Or is there another issue below that is not saving my SO # to the shipment record? I have the correct order number coming back into the code in my PXTrace. Any ideas? Thanks in advance!

  public PXProcessing<SOShipment, Where<SOShipment.operation, Equal<SOOperation.receipt>, And<Where<SOShipmentExt.usrToTransfer, IsNull, Or<SOShipmentExt.usrToTransfer.IsEqual<False>>>>>> shipments;
public createTransfers()
{
shipments.SetProcessCaption("Process");
shipments.SetProcessAllCaption("Process All");
shipments.SetProcessDelegate(CreateTransferOrders);
}

public static void CreateTransferOrders(List<SOShipment> shipments)
{
if (shipments.Count <= 0)
return;

WriteInformation("Start Processing");

foreach (SOShipment s in shipments)
{
try
{
using (var ts = new PXTransactionScope())
{
WriteInformation("Transaction");

var orderEntry = PXGraph.CreateInstance<SOOrderEntry>();
var shipment = PXGraph.CreateInstance<SOShipmentEntry>();
shipment.Document.Current = shipment.Document.Search<SOShipment.shipmentNbr>(s.ShipmentNbr);
SOOrder order = new SOOrder();
SOLine sOLine = new SOLine();

order.OrderType = "TR";
order.DestinationSiteID = 900;
order.CustomerID = s.CustomerID;

SOOrderExt orderExt = PXCache<SOOrder>.GetExtension<SOOrderExt>(order);

orderExt.UsrFromShipment = true;
orderExt.UsrFromShipNbr = s.ShipmentNbr.ToString();

order = orderEntry.Document.Insert(order);

PXTrace.WriteInformation(s.ShipmentNbr);
PXTrace.WriteInformation("Line Item Population");
PXSelectBase<SOShipLine> cmd = new PXSelectReadonly<SOShipLine, Where<SOShipLine.shipmentNbr, Equal<SOShipment.shipmentNbr.FromCurrent>>>(shipment);

foreach (SOShipLine sl in cmd.Select())
{
PXTrace.WriteInformation(sl.InventoryID.ToString());
sOLine.InventoryID = sl.InventoryID;
sOLine.OrderQty = sl.Qty;
sOLine.SiteID = sl.SiteID;
sOLine.UnitPrice = sl.UnitPrice;
sOLine.UnitCost = sl.UnitCost;
//sOLine.CustomerID = sl.CustomerID;
sOLine.OrderType = "TR";
sOLine = orderEntry.Transactions.Insert(sOLine);
}

orderEntry.Transactions.Cache.Update(orderEntry);
orderEntry.Actions.PressSave();

PXSelectBase<SOOrder> cmdOrder = new PXSelectReadonly<SOOrder, Where<SOOrderExt.usrFromShipment, Equal<SOShipment.shipmentNbr.FromCurrent>>>(shipment);

SOShipmentExt shipmentExt = PXCache<SOShipment>.GetExtension<SOShipmentExt>(shipment.Document.Current);

foreach (SOOrder or in cmdOrder.Select())
{
PXTrace.WriteInformation(or.OrderNbr);
shipmentExt.UsrToTransfer = true;
shipmentExt.UsrToTransferNbr = or.OrderNbr;
}
shipment.Document.Current = shipment.Document.Update(shipment.Document.Current);
shipment.Transactions.Cache.Update(shipment);
shipment.Actions.PressSave();
ts.Complete();

PXProcessing<SOShipment>.SetInfo(s.ShipmentNbr + " transfer created successfully.");
}
}
catch (Exception e)
{
PXProcessing<SOShipment>.SetError(shipments.IndexOf(s), e);
}
}
}

 

Hi @AJohnson,

Can you try if it works this way?

...
var shipmentRecord = shipment.Document.Current;
SOShipmentExt shipmentExt = PXCache<SOShipment>.GetExtension<SOShipmentExt>(shipmentRecord);

foreach (SOOrder or in cmdOrder.Select())
{
PXTrace.WriteInformation(or.OrderNbr);
shipmentExt.UsrToTransfer = true;
shipmentExt.UsrToTransferNbr = or.OrderNbr;
}
shipment.Document.Current = shipment.Document.Update(shipmentRecord);
...

 


Hello @Zoltan Febert,

I tried what you mentioned and a few other variations I could think of to no avail. For some reason I just can’t get a shipment to update through the code. Tested below: 

                        PXSelectBase<SOOrder> cmdOrder = new PXSelectReadonly<SOOrder, Where<SOOrderExt.usrFromShipNbr, Equal<SOShipment.shipmentNbr.FromCurrent>>>(shipment);

var sOShipment = shipment.Document.Current;

PXTrace.WriteInformation("3: " + sOShipment.ShipmentNbr);

SOShipmentExt shipmentExt = PXCache<SOShipment>.GetExtension<SOShipmentExt>(sOShipment);

foreach (SOOrder or in cmdOrder.Select())
{
PXTrace.WriteInformation("4: " + or.OrderNbr);
shipmentExt.UsrToTransfer = true;
shipmentExt.UsrToTransferNbr = or.OrderNbr;
}

shipment.Document.Update(sOShipment);
shipment.Transactions.Cache.Update(sOShipment);
shipment.Actions.PressSave();
ts.Complete();

Then after having no luck there, I put it in its own PXTransactionScope like below with no luck:

 foreach (SOShipment s in shipments)
{
try
{
using (var ts = new PXTransactionScope())
{
var shipment = PXGraph.CreateInstance<SOShipmentEntry>();

var cmd = new
SelectFrom<SOShipment>.
Where<SOShipment.shipmentNbr.IsEqual<@P.AsString>>.View(shipment);

SOShipment result = cmd.Select(s.ShipmentNbr);

PXTrace.WriteInformation("5: " + result.ShipDate.ToString());
SOShipmentExt shipmentExt = PXCache<SOShipment>.GetExtension<SOShipmentExt>(result);
shipmentExt.UsrToTransfer = true;
shipmentExt.UsrToTransferNbr = "123";
shipment.Document.Update(result);
shipment.Actions.PressSave();
ts.Complete();
PXProcessing<SOShipment>.SetInfo(s.ShipmentNbr + " transfer created successfully.");

}
}
catch (Exception e)
{
PXProcessing<SOShipment>.SetError(shipments.IndexOf(s), e);
}

Just hard coding some values to see if I can get the shipment record to update at all. I am wondering if SOShipmentEntry is the one to use here. Any ideas?

Thanks,

Adam


Hi @AJohnson,

Can you show me the source code of SOShipmentExt?


@Zoltan Febert,

Sorry, should have included that from the get-go. See below:

 public class SOShipmentExt : PXCacheExtension<PX.Objects.SO.SOShipment>
{
#region UsrToTransferNbr
[PXDBString]
[PXUIField(DisplayName = "To Transfer Nbr")]
public virtual string UsrToTransferNbr { get; set; }
public abstract class usrToTransferNbr : PX.Data.BQL.BqlString.Field<usrToTransferNbr> { }
#endregion

#region UsrToTransfer
[PXDBBool]
[PXUIField(DisplayName = "To Transfer")]
public virtual bool? UsrToTransfer { get; set; }
public abstract class usrToTransfer : PX.Data.BQL.BqlBool.Field<usrToTransfer> { }
#endregion
}

My SOOrder extension is almost identical as the goal is to link the shipment receipt to a SO transfer:

   public class SOOrderExt : PXCacheExtension<PX.Objects.SO.SOOrder>
{
#region UsrFromShipNbr
[PXDBString]
[PXUIField(DisplayName = "From Shipment Nbr")]
public virtual string UsrFromShipNbr { get; set; }
public abstract class usrFromShipNbr : PX.Data.BQL.BqlString.Field<usrFromShipNbr> { }
#endregion

#region UsrFromShipment
[PXDBBool]
[PXUIField(DisplayName = "From Shipment")]
public virtual bool? UsrFromShipment { get; set; }
public abstract class usrFromShipment : PX.Data.BQL.BqlBool.Field<usrFromShipment> { }
#endregion
}

Thanks!

Adam


@AJohnson, does to process save UsrToTransfer?


@Zoltan Febert,

Neither UsrToTransfer nor UsrToTransferNbr save currently. I tested with below and got the Shipment Description to update with a hard coded value. So, it's my extension that doesn't seem to want to update. I’ve gone through a bunch of iterations and below is just my current one. Not sure why though as it saved fine on the SOOrderExt. 

                        SOShipmentExt shipmentExt = PXCache<SOShipment>.GetExtension<SOShipmentExt>(shipment.Document.Current);

foreach (SOOrder or in cmdOrder.Select())
{
PXTrace.WriteInformation("4: " + or.OrderNbr);
shipmentExt.UsrToTransfer = true;
shipmentExt.UsrToTransferNbr = or.OrderNbr;
}
shipment.Document.Current.ShipmentDesc = "test";
shipment.Document.Update(shipment.Document.Current);
shipment.Transactions.Cache.Update(shipment.Document.Current);
shipment.Actions.PressSave();
ts.Complete();

 Thanks!

Adam


@AJohnson 

Maybe you use the wrong cache to get the extension? Try this:

SOShipmentExt shipmentExt = shipment.Document.Cache.GetExtension<SOShipmentExt>(shipment.Document.Current);

 


@Zoltan Febert 

Thanks for all the help, we finally got it working. Using your advice on the extension, I attempted again with no luck. I then noticed I had some odd error in my shipment extension and remembered I needed to suppress them through Acuminator. I dont know why this would have fixed it, but after republishing it with that it finally saved to those fields on the shipment. Thanks again for all your help, I am going to mark your last response as the best answer!

 


Reply