-->

Acumatica ERP redirect by condition after the page

2019-08-17 08:35发布

问题:

Is there any way to redirect from one custom page to another in case of some conditions when the first page is opened?

I have tried using PXRedirectByScreenIDException with RowSelected events but the exception is not being handled and redirect is not working.

protected void PrimaryView_RowSelected(PXCache sender,PXRowSelectedEventArgs e)
{
     if(some_expression)
     {
          throw new PXRedirectByScreenIDException("II999999", PXBaseRedirectException.WindowMode.Base, true);
     }
}

回答1:

Many Acumatica features work only in a narrow range of context. I noticed that Redirection Exception always work when executed in the context of an Action Event Handler . Presumably when executing those handlers the framework wraps the call around a catch block that specifically catch those redirection exception types and execute the redirection. In other contexts it probably doesn't catch this specific type and that results in a partially unhandled exception.

So my idea to redirect on page load is to throw the redirection exception inside an action event handler. The action need to be called from the UI context though otherwise it will still be unhandled. So I'll use JavaScript to call the action from UI on page load.

Graph extension that throws the redirection exception in the context of an action event handler:

public class SOOrderEntry_Extension : PXGraphExtension<SOOrderEntry>
{
  public PXAction<SOOrder> Redirect;

  [PXButton]
  public virtual IEnumerable redirect(PXAdapter adapter)
  {
      throw new PXRedirectByScreenIDException("SO302000", PXBaseRedirectException.WindowMode.Base, true);

      return adapter.Get();
  }
}

I used Add Controls to add the JavaScript control. For new pages (not customizing existing pages) it's generally easier to use Edit ASPX to add the JavaScript. The JavaScript function calls the redirection action event handler in the graph extension.

function Redirect_Initialize() { px_alls["ds"].executeCallback("redirect"); }

Last step is to call the JavaScript method when the page loads. I used CommandPerformed because I know from experience it will be reliably called. You could try Initialize or other types of Load events as the JavaScript entry point but I had less luck with those.

I tested this complete solution and it did redirect on load. Maybe it might perform too many callbacks when your condition will not end in a redirection, you'll have to test that and pick another entry point if necessary.

If no suitable entry point is found I would use a global JavaScript variable as a flag so that it calls the graph action event handler only once. Repeatedly calling a JavaScript function has very low overhead, repeatedly calling back the server (IIS/ASP.Net) carry a huge penalty.