Solved

'Unable to cast object of type 'PX.Objects.PO.POOrder' to type 'PX.Data.PXResult`2[PX.Objects.PO.POOrder,PX.Objects.PO.POLine]'.'

  • 1 March 2023
  • 8 replies
  • 428 views

Userlevel 3
Badge

Hi,

I’m new to acumatica. I have a customized screen. There is a button with an action which opens a pop-up window. In this pop-up window I have loaded data from both POOrder DAC and POLine DAC. To load data , I created a view using SelectFrom attribute .But when I try to add data from the pop-up window to the grid of my customized form, I’m getting below error.

 'Unable to cast object of type 'PX.Objects.PO.POOrder' to type 'PX.Data.PXResult`2[PX.Objects.PO.POOrder,PX.Objects.PO.POLine]'.

 

Can someone help me to solve this issue please?

Thank you.

see the below code for reference.


namespace GRIProformaInvoice.Extension
{

public class APProformaEntry_Extension : PXGraphExtension<APProformaEntry>
{


public SelectFrom<POOrder>.InnerJoin<POLine>.
On<POLine.orderNbr.IsEqual<POOrder.orderNbr>>.View POrdersView;


public PXAction<APProforma> MyAction;
[PXUIField(DisplayName = "Add Purchase", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select)]
[PXButton(CommitChanges = true)]
public virtual IEnumerable myAction(PXAdapter adapter)
{

if (POrdersView.AskExt() == WebDialogResult.OK)
{


return InsertSelectedLines(adapter);


}
return adapter.Get();


}

public virtual IEnumerable InsertSelectedLines(PXAdapter adapter)
{
if (POrdersView != null && POrdersView.Cache.Cached != null)
{
int lineNbr = Base.APProformaItems.Select().Count + 1;

foreach (PXResult<POOrder, POLine> result in POrdersView.Cache.Cached)
{
POOrder order = result.GetItem<POOrder>();
POLine line = result.GetItem<POLine>();

APProformaItemList toBeInserted = new APProformaItemList();
toBeInserted.Ponbr = "aa";
toBeInserted.LineNbr = lineNbr++;

toBeInserted.POrderQty = 30;
toBeInserted.Itemid = 25;
toBeInserted.Description = line.TranDesc;

toBeInserted = Base.APProformaItems.Insert(toBeInserted);
Base.APProformaItems.Update(toBeInserted);


}
}
return adapter.Get();

}


}


 

icon

Best answer by oshadarodrigo64 23 March 2023, 15:02

View original

8 replies

Userlevel 7
Badge +17

Hi @oshadarodrigo64  Can you please try like below example with the PXResult?

 

 foreach (PXResult<SOShipment, SOShipLine, SOLine> record in results)
{
SOShipment shipment = (SOShipment)record;
SOShipLine shipmentLine = (SOShipLine)record;
SOLine sOLine = (SOLine)record;
}
Userlevel 3
Badge

Hi @Naveen Boga ,

Thank you for the response, I tried your solution but still I’m getting the same error.

Userlevel 4
Badge +2

Hey @oshadarodrigo64  Try the following 

foreach (var record in POrdersView.Cache.Cached)

{

var POOrder = result.RowCast<POOrder>().First();

var POLine = result.RowCast<POLine>().First();

...

}

Userlevel 7
Badge +8

You missing a join in your public view POrdersView on OrderType. Not sure but could be the problem.

Userlevel 3
Badge

Hi @Patrick Chen , @aaghaei 

I tried the following way to avoid the error and it worked for me.

          if (POrdersView != null)
{
int lineNbr = Base.APProformaItems.Select().Count + 1;

foreach (PXResult<POOrder, POLine> result in POrdersView.Select())
{
POOrder order = result;
POLine line = result;


if (order.Selected == false) continue;

{
APProformaItemList toBeInserted = new APProformaItemList();
toBeInserted.Ponbr = "aa";
toBeInserted.LineNbr = lineNbr++;
toBeInserted.POLineNbr = line.LineNbr;
toBeInserted.POrderQty = 30;
toBeInserted.Itemid = 25;
toBeInserted = Base.APProformaItems.Insert(toBeInserted);
Base.APProformaItems.Update(toBeInserted);

}
}


but now the issue is when I select one value from orderNbr and clicked add button, it inserts all the values from the same orderNbr instead of one selected item. However, when I open the smart panel again, I can see that all the values from same orderNbr have been selected automatically like below.

 How can I solve this?

Thank you.

Userlevel 7
Badge +17

@oshadarodrigo64  Not sure why it is inserting the duplicate values, as a safety check if same Order existing in that table write an update query instead insert query, so that you can avoid the duplicate values from the above list.

Userlevel 3
Badge

Hi,

The above cast object of type error is gone after defining the view like below.

 

public SelectFrom<POLine>

          .InnerJoin<POOrder>.On<POLine.orderNbr.IsEqual<POOrder.orderNbr> 

          .And<POLine.orderType.IsEqual<POOrder.orderType>>>

          .View POrdersView;

 

and also credit goes to @aaghaei to mentioning this.

Regards.

Userlevel 7
Badge

Thank you for sharing the solution with the community @oshadarodrigo64 !

Reply


About Acumatica ERP system
Acumatica Cloud ERP provides the best business management solution for transforming your company to thrive in the new digital economy. Built on a future-proof platform with open architecture for rapid integrations, scalability, and ease of use, Acumatica delivers unparalleled value to small and midmarket organizations. Connected Business. Delivered.
© 2008 — 2024  Acumatica, Inc. All rights reserved