function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Raghu RamanujamRaghu Ramanujam 

Stop Redirecting to Home Page

Hello Gurus,

I wanted to overide Edit Opportunity page with a custom Visualforce Page for particular record type. (Reason for this is, want to display around 10 fields in a new section, based on Opportunity StageName)

Done the Overriding part. (with the help of this blog from Jeff   ..http://blog.jeffdouglas.com/2009/06/26/overriding-standard-links-with-visualforce-pages/)

For that particulat record type the custom visualforce page is displayed and the new section appears when the Stage is changed to a particualr one.But the problem is, for other Record types standard page is displayed. when I click on edit and later click Cancel button the users are redirected to Home Page instead of detail page ???
Any idea my this is happening.

Please see my code below ..

Visualforce Page One ( used to override the Edit button)
<apex:page standardController="Opportunity" extensions="DispatcherOpportunityViewController"  
    action="{!nullValue(redir.url, urlFor($Action.Opportunity.Edit, Opportunity.id, null, true))}">
</apex:page>
Controller Class
public with sharing class DispatcherOpportunityViewController {
    
    public Boolean Takeon { get; set; }
    private final Opportunity oppty;
    public String currentRecordId {get;set;}
    public Opportunity oppt{get;set;}

    public DispatcherOpportunityViewController(ApexPages.StandardController controller) {
         
          this.controller = controller;
          takeon = false;
          Opportunity   oppt = [Select id, stageName From Opportunity Where Id = :ApexPages.currentPage().getParameters().get('id')];
           if(oppt.StageName == '10. Deal Paid Out/Win'){
             takeon = true;  
          }
        }
     
    public PageReference getRedir() {
         Opportunity p = [Select id, recordtypeid From Opportunity Where Id = :ApexPages.currentPage().getParameters().get('id')];
         PageReference newPage;
         if (p.recordtypeid == '012w0000000MO5pAAG') {
            newPage = Page.NewEdit_Opportunity;
            newPage.getParameters().put('id', p.id );
        } else {
            newPage = new PageReference('/' + p.id + '/e');
            newPage.getParameters().put('nooverride', '1');
        }
        newPage.getParameters().put('id', p.id );
        return newPage.setRedirect(true);
    }
    
       public PageReference cancel(){
        //PageReference pageref = new PageReference('/006/o');
        ///PageReference pageref = new PageReference('/' + controller.getid());
        ///pageref.setRedirect(true);
        ///return pageref;
        return controller.cancel();
        //PageReference pRef;
        //pRef = controller.cancel();
       // pRef.setRedirect(true);
       // return pRef;
        //return null;
       }

       public PageReference stage(){
        Opportunity opp3 = [Select id, stageName From Opportunity Where Id = :ApexPages.currentPage().getParameters().get('id')];
        system.debug('StageName3 ' + opp3);
        system.debug('apex ' + ApexPages.currentPage().getParameters());
        SObject record = controller.getrecord();
        if(record instanceof Opportunity){
            Opportunity opp = (Opportunity) controller.getrecord();
            System.debug('StageName ' + opp.StageName);
            if(Opp.StageName == '10. Deal Paid Out/Win'){
                takeon = true;      
            }else{
                takeon = false;
            }
        }
        return null;
       }
        private final ApexPages.StandardController controller;
}

Visualforce Page 2 (Edit Page)

<apex:page standardController="Opportunity" showHeader="true" sidebar="true" tabStyle="Opportunity" extensions="DispatcherOpportunityViewController"
          action="{!stage}">

<apex:SectionHeader title="Edit Opportunity" subtitle="{!Opportunity.name}"/>
<apex:form >
 <apex:pageBlock title="Edit Opportunity" id="thePageBlock" mode="Edit">
     <apex:PageBlockButtons >
       <apex:commandButton value="Save" action="{!save}"/>
       <apex:commandButton value="Cancel" action="{!cancel}" immediate="true"/>
     </apex:PageBlockButtons>
     <apex:pageBlockSection title="Opportunity Information" columns="2">
     <apex:outputField value="{!Opportunity.OwnerId}"/>
     <apex:outputField value="{!Opportunity.RecordType.Name}"/>
     
     <apex:inputfield Label="Opportunity Name" value="{!Opportunity.Name}"/>
     <apex:inputfield Label="Close Date" value="{!Opportunity.CloseDate}"/>     
     
     <apex:inputtext Label="Account Name" value="{!Opportunity.account.name}"/>
     <apex:pageblockSectionItem >
        <apex:outputLabel value="Stage" for="Stage" />
            <apex:actionRegion >
                 <apex:inputField value="{!Opportunity.StageName}" id="Stage" required="true">
                  <apex:actionSupport event="onchange" rerender="TakeOn" action="{!stage}"/>
                 </apex:inputField>
            </apex:actionRegion>
    </apex:pageblockSectionItem>

     <apex:inputfield Label="Referred To Introducer" value="{!Opportunity.Referred_to_Introducer__c}"/>
     <apex:inputfield Label="Reason Lost" value="{!Opportunity.Reason_Won_Lost__c}"/>
     <apex:inputfield Label="Description" value="{!Opportunity.Description}"/>
     <apex:inputfield Label="Name Of Introducer Referred" value="{!Opportunity.Name_of_introducer_referred__c}"/>
     
     </apex:pageBlockSection>
     <apex:outputPanel id="TakeOn" >
         <apex:pageBlockSection title="Take On Details" columns="2" rendered="{!takeon}">
            
            <apex:inputfield Label="Executed Date" value="{!Opportunity.Executed_Date__c}"/>
            <apex:inputfield Label="Facility Type" value="{!Opportunity.Facility_Type__c}"/>
            <apex:inputfield Label="Turn Over" value="{!Opportunity.Turnover_HCIF__c}"/>
            <apex:inputfield Label="VOB" value="{!Opportunity.VOB__c}"/>
            <apex:inputfield Label="1st Payment" value="{!Opportunity.X1st_Payment__c}"/>
            <apex:inputfield Label="Setup Fees" value="{!Opportunity.Setup_Fee__c}"/>
            <apex:inputfield Label="Total Income" value="{!Opportunity.Total_Income__c}"/>
            </apex:pageBlockSection>
     </apex:outputPanel>
 </apex:pageBlock>
</apex:form>
</apex:page>