I have the following Visualforce page that overrides the "View" action of a standard object:
<apex:page standardController="Opportunity" extensions="OpportunityDispatcherCx" action="{!redirect}"/>
This is my controller:
public class OpportunityDispatcherCx {
// Opportunity Variable
Opportunity oOpportunity {get;set;}
public OpportunityDispatcherCx(ApexPages.standardController stdController) {
oOpportunity = [Select Id, Amount
From Opportunity
Where Id = :stdController.getId()];
}
public PageReference redirect() {
// Construct a redirection url
String redirectUrl = '';
// Make your checks
if(oOpportunity.Amount < 100) {
redirectUrl = '/apex/errorPage?id=' + oOpportunity.Id;
}else {
redirectUrl = '/' + oOpportunity.Id;
}
// Set a pageReference for redirection
PageReference pageRef = new PageReference(redirectUrl);
// Return it for the redirection
return pageRef;
}
}
However, when an Opportunity amount is over 100, the code will be called again and again for every successful redirection, leading to infinite recursion. What is the solution?