[SalesForce] New button redirecting to vf page based on Record Type

I have multiple Record Types for contact. when the user clicks on 'New Contact' button for a specific record type it should redirect to a custom edit vf page. All the remaining record types should use the standard layouts as per the assignment.

Here are my questions:

  1. Do I need to override the New button with the vf page? If I'm doing this overriding of button the vf page is showing to all record types irrespectively.
  2. Is something that I can handle in the redirect method without overriding the new method?
  3. Can we do it by querying the recordtype sobject instead of hardcoding?

Here is my code:

Page:

<apex:page standardcontroller="Contact" extensions="CustomEditPageController" sidebar="false" tabstyle="Contact" action="{!redirect}">

Controller:

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

Best Answer

For your issue#1, when you override the New Action with the custom VF page there is a checkbox "Skip recordtype selection page" there that controls whether the default recordtype selection should be displayed or not. You need to check it. Then as Shailesh mentioned, in your custom VF page you will have to present list of recordtypes available to the user and based on recordtype selected route the user to either custom edit page or the standard page layout. Hope this helps.