Skip to main content
Answer

Class Extension is not initiated/called in Graph Extension

  • May 10, 2024
  • 7 replies
  • 127 views

aaghaei
Captain II
Forum|alt.badge.img+10

Hello All,

 

I have created a class Extension to override a methd and then I want to call that class extension in a graph extension that I am overriding an action of the graph.

Here is my Graph Ext:

using System.Collections;
using PX.Data;
using PX.Objects.EP;

namespace MyProject
{
public class MyTimeCardMaintExt : PXGraphExtension<TimeCardMaint>
{
public static bool IsActive() => true;

public MyEmployeeCostEngineExt MyEmployeeCostEngine;

public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);

[PXOverride]
[PXButton]
public virtual IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
// Do some stuff
return baseMethod?.Invoke(adapter);
}
}
}

and here is my Class Ext:

using System;
using PX.Data;
using PX.Objects.EP;

namespace MyProject
{
public class MyEmployeeCostEngineExt : EmployeeCostEngine
{
public MyEmployeeCostEngineExt(PXGraph graph) : base(graph) { }

public static bool IsActive() => true;

public decimal? OvertimeMultiplier { get; set; }

//**************************************** Methods Override ****************************************//
public delegate decimal GetOvertimeMultiplierDelegate(string earningTypeID, Rate employeeRate);

[PXOverride]
public virtual decimal GetOvertimeMultiplier(string earningTypeID, Rate employeeRate, GetOvertimeMultiplierDelegate baseMethod)
{
// Do some stuff
return baseMethod?.Invoke(earningTypeID, employeeRate);
}
}
}

My problem is that the class extension “MyEmployeeCostEngineExt” is not called. If I want to for the class initialization as follow in my Graph Ext, the Base is null.

public MyTimeCardMaintExt()
{
MyEmployeeCostEngine = new MyEmployeeCostEngineExt(Base);
}

Any idea why the class Extension is not called and what I can do about it?

Best answer by Dmitrii Naumov

@aaghaei you need to override CreateEmployeeCostEngine() method. 

Otherwise the base graph will still create an object of the base EmployeeCostEngine and will use it instead of your derived one. 

7 replies

aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • May 11, 2024

UPDATE:

I added the below code to my Graph Ext.

public override void Initialize()
{
base.Initialize();
HCLEmployeeCostEngine = new HCLEPEmployeeCostEngine(Base);
}

I also tried overriding “GetOvertimeMultiplier” as follows:

[PXOverride]
public override decimal GetOvertimeMultiplier(string earningTypeID, Rate employeeRate)
{
// Do some stuff
base.GetOvertimeMultiplier(earningTypeID, employeeRate);
}

The Base is not null anymore but still the overridden method “GetOvertimeMultiplier” in the Class Ext “MyEmployeeCostEngineExt” is not called.


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7

@aaghaei EmployeeCostEngine is not a PXGraph. The PXOverride only works for graphs.


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • May 14, 2024

Thank you @Dmitrii Naumov for the clarfification but even by removing the [PXOverrode] still my overridden method is not called. Here is the little class override as I have it now. Any thoughs why the override is not executed?

using System;
using PX.Data;
using PX.Objects.EP;

namespace MyProject
{
public class MyEmployeeCostEngineExt : EmployeeCostEngine
{
public MyEmployeeCostEngineExt(PXGraph graph) : base(graph) { }

public static bool IsActive() => true;

public override decimal GetOvertimeMultiplier(string earningTypeID, Rate employeeRate)
{
// Do some stuff
base.GetOvertimeMultiplier(earningTypeID, employeeRate);
}
}
}

 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7

@aaghaei I think you need to override 

public virtual EmployeeCostEngine CreateEmployeeCostEngine()

from TimeCardMaint and return your overridden EmployeeCostEngine class from there. 

That should do the trick.

 

BTW, instead of writing code in the graph extension constructor, you can override Initialize method, which server the same purpose but already has Base graph defined. 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • May 15, 2024

Thank you for the response @Dmitrii Naumov Below code is my underestanding of your comments but still I can’t make the Acumatica framework to call the “GetOvertimeMultiplier” override. Any idea what else I am missing here and any chance if you kindly try to see if you can make it work?

using System;
using System.Collections;
using PX.Data;
using PX.Objects.EP;

namespace MyProject
{
public class MyTimeCardMaintExt : PXGraphExtension<TimeCardMaint>
{
public static bool IsActive() => true;

public MyEmployeeCostEngineExt MyEmployeeCostEngine;

public override void Initialize()
{
base.Initialize();
MyEmployeeCostEngine = new MyEmployeeCostEngineExt(Base);
}

public delegate IEnumerable ReleaseDelegate(PXAdapter adapter);

[PXOverride]
[PXButton]
public virtual IEnumerable Release(PXAdapter adapter, ReleaseDelegate baseMethod)
{
// Do some stuff
return baseMethod?.Invoke(adapter);
}


public class MyEmployeeCostEngineExt : EmployeeCostEngine
{
public MyEmployeeCostEngineExt(PXGraph graph) : base(graph) { }

public override decimal GetOvertimeMultiplier(string earningTypeID, Rate employeeRate)
{
// Do some stuff
base.GetOvertimeMultiplier(earningTypeID, employeeRate);
}
}
}
}

 


Dmitrii Naumov
Acumatica Moderator
Forum|alt.badge.img+7
  • Acumatica Moderator
  • Answer
  • May 15, 2024

@aaghaei you need to override CreateEmployeeCostEngine() method. 

Otherwise the base graph will still create an object of the base EmployeeCostEngine and will use it instead of your derived one. 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • May 15, 2024

@Dmitrii Naumov I really appreciate your help. The overriden class is called when I override the CreateEmployeeCostEngine(). Thanks again.