Skip to main content
Question

Issue with executing remainder of the code in Delegate

  • June 3, 2024
  • 6 replies
  • 62 views

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

Hello all,

I have a Delegate which is triggered and also executes the base method from inside the delegate. The problem I have is that the remainder of the code after calling the base method is not executed. Any idea why? Here is the code. The breakpoint on “baseMethod...” gets hit but on “if (...” does not.

public class MyEPApprovalProcessExt : PXGraphExtension<EPApprovalProcess>
{
#region editDetailDelegate
public delegate IEnumerable editDetailDelegate(PXAdapter adapter);
[PXOverride]
[PXEditDetailButton]
public virtual IEnumerable editDetail(PXAdapter adapter, editDetailDelegate baseMethod)
{
baseMethod?.Invoke(adapter);

// Nothing is executed from here forward.
if (Base.Records.Current.RefNoteID.Value != null)
{
// Do some stuff
}

return adapter.Get();
}
#endregion
}

Any help is appreciated

6 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3

Hi @aaghaei,

If Construction module is enabled, then there is a PXRedirectRequiredException in editDetail. You need to catch it and throw it again after your stuff.

try
{
return baseMethod.Invoke(adapter);
}
catch (PXRedirectRequiredException redirect)
{
if (Base.Records.Current.RefNoteID.Value != null)
{
// Do some stuff
}

throw;
}

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • June 4, 2024

​Thank you for the reply @Zoltan Febert 

With the changes you suggested, my issue is swapped. Basically, when I double click on a record in Approvals screen, the editDetail method takes me to the document. In my original code, the custom code after the baseMethod was not executed. With the changes you suggested, now my code is executed but I am not directed to the Document before executing my custom code.

Basically, what I am trying to do is to open a new window automatically after I am redirected to the document from the Approvals screen. I am trying to open a custom screen, but I am putting a redirect to ProjectEntry so you don’t receive error if you want to test it. Here is the simplified code if you or someone else kindly can give it a try and help me out.

public class MyEPApprovalProcessExt : PXGraphExtension<EPApprovalProcess>
{
#region editDetailDelegate
public delegate IEnumerable editDetailDelegate(PXAdapter adapter);
[PXOverride]
[PXEditDetailButton]
public virtual IEnumerable editDetail(PXAdapter adapter, editDetailDelegate baseMethod)
{
try
{
return baseMethod.Invoke(adapter);
}
catch (PXRedirectRequiredException redirect)
{
if (Base.Records.Current.RefNoteID.Value != null)
{
ProjectEntry target = PXGraph.CreateInstance<ProjectEntry>();
target.Project.Current = target.Project.Search<PMProject.contractID>(0);
throw new PXPopupRedirectException(target, PX.Objects.PM.Messages.Project, true);
}

throw;
}
}
#endregion
}

 


Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3

Hi @aaghaei,

So actually you want to throw two different exceptions from the same function?
There is an AggregateException class in c#, but I am not sure the framework is prepared to handle it.

public class MyEPApprovalProcessExt : PXGraphExtension<EPApprovalProcess>
{
#region editDetailDelegate
public delegate IEnumerable editDetailDelegate(PXAdapter adapter);
[PXOverride]
[PXEditDetailButton]
public virtual IEnumerable editDetail(PXAdapter adapter, editDetailDelegate baseMethod)
{
var exceptions = new List<Exception>();
try
{
return baseMethod.Invoke(adapter);
}
catch (PXRedirectRequiredException redirect)
{
exceptions.Add(redirect);
if (Base.Records.Current.RefNoteID.Value != null)
{
ProjectEntry target = PXGraph.CreateInstance<ProjectEntry>();
target.Project.Current = target.Project.Search<PMProject.contractID>(0);
var popup = new PXPopupRedirectException(target, PX.Objects.PM.Messages.Project, true);
exceptions.Add(popup);

throw new AggregateException(exceptions);
}

throw;
}
}
#endregion
}

 


aaghaei
Captain II
Forum|alt.badge.img+10
  • Author
  • Captain II
  • June 4, 2024

Thank you @Zoltan Febert 

the code is being executed up to the point of aggregation 

 

but when throws it I receive this errors that “One or more errors occurred.” without any further details.


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • July 11, 2024

Hi @aaghaei were you able to find a solution? Thank you!


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

Hi Chris, thanks for the follow up. No, not really.