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);
}
}
Many Acumatica features work only in a narrow range of context. I noticed that
Redirection Exception
always work when executed in the context of anAction 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:
I used
Add Controls
to add the JavaScript control. For new pages (not customizing existing pages) it's generally easier to useEdit ASPX
to add the JavaScript. The JavaScript function calls the redirection action event handler in the graph extension.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 tryInitialize
or other types ofLoad
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.