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
BManojKumarBManojKumar 

How to redirect the page to visual force page based on the record type selection

Hi All,

 

I have a set of record types for opportunities in my organization. I have a visual force page. I would like to redirect the user to the visual force page whenever a particular record type is selected in the drop down list.

 

Can anyone help me out , how to do this ?

 

Thank in Advance.

Good Day.

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

In VF page add action attribute in apex:page tag

 

<apex:page standardcontroller="Opportunity" extensions="redirect" action="{!redirect}>
.
.
.
</apex:page>

 

In the extensions class create a method Redirect containing the logic

 

public pagereference Redirect()
{
     If(ApexPages.currentPage().getParameters().get('RecordType') != '0000000000000000')
     {
           String hostname = ApexPages.currentPage().getHeaders().get('Host'); 
           String optyURL2 = 'https://'+hostname+'/'+'/00Q/e?nooverride=1';
           pagereference pageref = new pagereference(optyURL2);
           pageref.setredirect(true);
           return pageref;
     }
     else
          return null;
}

 

 

This should do it!!

Let me know if any issues

All Answers

sinsiterrulezsinsiterrulez

Hi,

You can use the recordtype ID to redirect.

Just override the New button with your VF page & add the redirection logic in that page. To redirct to standard SFDC page you can add the override parameter in URL

BManojKumarBManojKumar
Hi, It sounds good. Can u give some example code for this ? Because I would like the user to direct only for the particular recordtype. Thanks Good Day
sinsiterrulezsinsiterrulez

In VF page add action attribute in apex:page tag

 

<apex:page standardcontroller="Opportunity" extensions="redirect" action="{!redirect}>
.
.
.
</apex:page>

 

In the extensions class create a method Redirect containing the logic

 

public pagereference Redirect()
{
     If(ApexPages.currentPage().getParameters().get('RecordType') != '0000000000000000')
     {
           String hostname = ApexPages.currentPage().getHeaders().get('Host'); 
           String optyURL2 = 'https://'+hostname+'/'+'/00Q/e?nooverride=1';
           pagereference pageref = new pagereference(optyURL2);
           pageref.setredirect(true);
           return pageref;
     }
     else
          return null;
}

 

 

This should do it!!

Let me know if any issues

This was selected as the best answer
SteveBowerSteveBower

 

This is similar to the previous posters answer without needing a controller.   

 

The record type that has been selected is being passed to whatever page is used for the New function with the record type id being passed in a "RecordType" parameter on the URL.

 

Let's assume that the ID of the RecordType for the Opportunity for which you want to use your own VF page is '012A0000000RFok'.  

 

So, you create a VF page called "MyNewOpportunity" and use it as the override for the New operation on Opportunities.   

 

<apex:page standardController="Opportunity" action="{!if($CurrentPage.parameters.RecordType != '012A0000000RFok',urlfor('/006/e',null,[RecordType=$CurrentPage.parameters.RecordType],true),'')}" >
...
... the rest of your page
...
</apex:page>

 

 

 

Which is essentially saying, if the RecordType parameter we've been passed is NOT the one for which you want to use this VF page, then redirect to a URL which is built for the New Opportunity action (/006/e), however don't override the default New action with your VF page (true), and pass along the RecordType that the user selected in the first place (RecordType=$CurrentPage.parameters.RecordType)

 

Best. Steve.

 

 

BManojKumarBManojKumar

Hi SteveBower / sinsiterrulez , Thanks for your help.. Its fix my issue..

 

Have a nice day..

Sekha(Devlope)rSekha(Devlope)r

I actually found a way to redirect user to the specific Visual Force page based  on profile attached record type. I have achived this using Custom Settings Object. Created Custom Setting Object as list with hirarchy and visabulity public. and Created Record Type, PageName fields. and used the below code to redirect user based on the record type.

 

Controller

******************************************************************************************************************************

public with sharing class OpportunityCSController{
public CreateOpportunity__c recordtypemapping ;
public Set<String> recordtypenames  = new Set<String>();
    public OpportunityCSController(ApexPages.StandardController controller) {
    recordtypemapping  = CreateOpportunity__c.getInstance(UserInfo.getProfileId());
        System.debug(recordtypemapping);
}
    String recType;
    public List<SelectOption> getrectypes() {
       System.debug(recordtypemapping);
        String[] recordtypes = new List<String>();
        recordtypes =  recordtypemapping.recordtype__c.split(',',recordtypemapping.recordtype__c.length());
        if(recordtypes.size()==0){
            recordtypes.add(recordtypemapping.recordtype__c);
        }
        recordtypenames.addAll(recordtypes);
        List<SelectOption> options = new List<SelectOption>();
        for(RecordType rt:[select id,name from RecordType where sobjecttype = 'CRM_Opportunity__c'  and DeveloperName IN: recordtypenames]){
        options.add(new SelectOption(rt.name,rt.name));
    }
        return options;
    }
        public String getRecordType() {
        return recType;
    }
        public void setRecordType(String recType) {
        this.recType= recType;
    }     
        public PageReference save()
    {
         string idr;
         string st ='';
         system.debug('sttype'+st);
         idr=ApexPages.currentPage().getParameters().get('Contactid');
         string qrystring = [SELECT PageName__c FROM CreateOpportunity__c WHERE RecordType__c =:st][0].PageName__c;
         PageReference home = new PageReference(qrystring+idr);
         home.setRedirect(true);
    return home;
    }
   public PageReference cancel()
     {
     string idr;
     idr=ApexPages.currentPage().getParameters().get('Contactid');
     PageReference home = new PageReference('/'+idr);
     home.setRedirect(true);
     return home;
     }
          
     public string rtps {get; set;}
 
}

 

********************************************************************************************************************

Visual Force Page

 

<apex:page standardController="CreateOpportunity__c" extensions="OpportunityCSController" action="{!save}">
    <apex:sectionHeader subtitle="New Opportunity"/>
    <apex:form ><apex:pageBlock title="{!$ObjectType.CRM_Opportunity__c.label} New" mode="new">
    <apex:pageBlockButtons >
            <apex:commandButton action="{!Save}" value="Next"/>
            <apex:commandButton action="{!cancel}" value="Cancel"/>
        </apex:pageBlockButtons>
            <apex:pageBlockSection showHeader="true" title="Select Record Type" columns="2">
                            <apex:selectList value="{!RecordType}" multiselect="false"  size="1">
                <apex:selectOptions value="{!rectypes}"/>
                <apex:actionSupport event="onchange" rerender="bb" status="status"/>                
            </apex:selectList>
            </apex:pageBlockSection>
                     </apex:pageBlock>
    </apex:form>
</apex:page>