Solved

Override method in child extention


Userlevel 4
Badge +1

Hi, I’m trying to override a method in a graph extension, then override it again in a second extension. I’ve simplified the code to provide an example.

public class SOShipmentEntryWrite : PXGraphExtension<PX.Objects.SO.SOShipmentEntry>
{
[PXOverride]
public void ConfirmShipment(SOOrderEntry docgraph, SOShipment shipment)
{
Base.ConfirmShipment(docgraph, shipment);
//Write some info
}
}

public class SOShipmentEntryRead : PXGraphExtension<PX.Objects.SO.SOShipmentEntryWrite, PX.Objects.SO.SOShipmentEntry>
{
[PXOverride]
public void ConfirmShipment(SOOrderEntry docgraph, SOShipment shipment)
{
Base1.ConfirmShipment(docgraph, shipment);
//Read the info
}
}

There are 2 classes SOShipmentEntryWrite and SOShipmentEntryRead. SOShipmentEntryRead is an extension of SOShipmentEntryWrite.

My hope was that ConfirmShipment in SOShipmentEntryRead would override ConfirmShipment in SOShipmentEntryWrite.
Then I could use Base1.ConfirmShipment in SOShipmentEntryRead to call ConfirmShipment in SOShipmentEntryWrite.

What actually happens is that ConfirmShipment in SOShipmentEntryWrite runs first and then ConfirmShipment in SOShipmentEntryRead runs second. Both classes have overridden the base method in SOShipmentEntry and are running there version of ConfirmShipment.

Is there a way for a child extension to override an overridden method?

 

 

icon

Best answer by NicholasBova52 1 May 2024, 16:56

View original

2 replies

Userlevel 2
Badge +1

@stephenward03 In order to get the override of the method working how you want, you will need to include a delegate matching the method signature of the base “ConfirmShipment” method, and then add that delegate as the final parameter in your PXOverride method. Then, you can call the delegate parameter from your PXOverride method to run the code from the lower level extensions and base graph. Doing it this way, the order of execution will follow from the highest level extension, to lower levels, down to the base graph. The code will looks something like this:

public class Delegates
{
public delegate void ConfirmShipmentDelegate(SOOrderEntry docgraph, SOShipment shipment);
}

public class SOShipmentEntryWrite : PXGraphExtension<SOShipmentEntry>
{
[PXOverride]
public void ConfirmShipment(SOOrderEntry docgraph, SOShipment shipment, Delegates.ConfirmShipmentDelegate baseMethod)
{
baseMethod(docgraph, shipment);
//Write some info
}
}

public class SOShipmentEntryRead : PXGraphExtension<SOShipmentEntryWrite, SOShipmentEntry>
{
[PXOverride]
public void ConfirmShipment(SOOrderEntry docgraph, SOShipment shipment, Delegates.ConfirmShipmentDelegate baseMethod)
{
baseMethod(docgraph, shipment);
//Read the info
}
}

For more information about this behavior, see this help article here: To Override a Virtual Method

Userlevel 4
Badge +1

It’s the perfect answer and worked on the first attempt. Thank you so much for your help.

I had tried to use delegates previously but I defined one delegate in each class, your solution of defining a delgate in a public class which the other classes can share is a great solution.

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