Skip to main content
Answer

Override method in child extention

  • May 1, 2024
  • 2 replies
  • 395 views

Forum|alt.badge.img+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?

 

 

Best answer by NicholasBova52

@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

2 replies

NicholasBova52
Acumatica Employee
Forum|alt.badge.img+1
  • Acumatica Employee
  • Answer
  • May 1, 2024

@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


Forum|alt.badge.img+1
  • Author
  • Varsity III
  • May 1, 2024

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.