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
Nkosilathi NcubeNkosilathi Ncube 

Visualforce Page override for one Record Type

On the Contact object I have two Record types "A" and "B", I have created a visualforce page which I want to use for record type A not for both I want to keep B default, please help
Best Answer chosen by Nkosilathi Ncube
UC InnovationUC Innovation
I see.  The Visualforce Page (ContactPageRedirect) would be the same.  We just need to update the controller to be the following instead:
 
public with sharing class ContactPageRedirect {

    Contact cont {get; set;}
    Id recordTypeA;

    public ContactPageRedirect(ApexPages.StandardController stdController) {
        cont = (Contact)stdController.getRecord();
        
        recordTypeA = [SELECT Id
                       FROM RecordType
                       WHERE SObjectType = 'Contact' AND Name = 'ContactRecordTypeA' LIMIT 1].Id;	                             

    }   

    public PageReference continueNextPage() {
        PageReference newPage;

		newPage = Page.ContactRecordTypeA;
        
        if (cont.RecordTypeId != recordTypeA)
        {
            String retUrl = cont.Id;
            newPage = new PageReference('/' + retUrl);
        }
        
        if (ApexPages.currentPage().getParameters().get('retURL') != null) {
            newPage.getParameters().put('retURL', ApexPages.currentPage().getParameters().get('retURL'));
        }
               
        newPage.getParameters().put('nooverride', '1');
        
        return newPage;
    }
}

Thus, instead of making it go to the standard edit page ('/e'), we just use the record ID as the URL instead.

Hope that helps!

All Answers

UC InnovationUC Innovation
You can do something like this.  In the Visualforce Page (called ContactPageRedirect):
 
<apex:page standardController="Contact" extensions="ContactPageRedirect" action="{!continueNextPage}">

</apex:page>

In the controller:
 
public with sharing class ContactPageRedirect {

    Contact cont {get; set;}
    Id recordTypeA;

    public ContactPageRedirect(ApexPages.StandardController stdController) {
        cont = (Contact)stdController.getRecord();
        
        recordTypeA = [SELECT Id
                       FROM RecordType
                       WHERE SObjectType = 'Contact' AND Name = 'ContactRecordTypeA' LIMIT 1].Id;	                             

    }   

    public PageReference continueNextPage() {
        PageReference newPage;

		newPage = Page.ContactRecordTypeA;
        
        if (cont.RecordTypeId != recordTypeA)
        {
            String retUrl = '003/e';
            newPage = new PageReference('/' + retUrl);
        }
        
        if (ApexPages.currentPage().getParameters().get('RecordType') != null) {
            newPage.getParameters().put('RecordType', ApexPages.currentPage().getParameters().get('RecordType'));
        }
        
        if (ApexPages.currentPage().getParameters().get('retURL') != null) {
            newPage.getParameters().put('retURL', ApexPages.currentPage().getParameters().get('retURL'));
        }
        
        if (ApexPages.currentPage().getParameters().get('ent') != null) {
            newPage.getParameters().put('ent', ApexPages.currentPage().getParameters().get('ent'));
        }
        
        newPage.getParameters().put('nooverride', '1');
        
        return newPage;
    }
}

Replace 'ContactRecordTypeA' with the actual name of your record type.  Repalce Page.ContactRecordTypeA with the name of your Visualforce Page.

You'll need to override the New button on the Contact to use the above VF page.  Make sure the 'Skip Record Type Selection Page' checkbox is NOT checked.

https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_controller_customize_override.htm
Nkosilathi NcubeNkosilathi Ncube
thanks UC, your solution works for the New button, what I am trying to achieve is have the VIEW to be different Visualforce Page, eg the VIEW for Recordtype A is a Visualforce Page and Record type B is standard Layout, I hope I am making sense cause at the moment when I override the VIEW with the VisualforcePage, both record types are assigned the same visualforce page
UC InnovationUC Innovation
I see.  The Visualforce Page (ContactPageRedirect) would be the same.  We just need to update the controller to be the following instead:
 
public with sharing class ContactPageRedirect {

    Contact cont {get; set;}
    Id recordTypeA;

    public ContactPageRedirect(ApexPages.StandardController stdController) {
        cont = (Contact)stdController.getRecord();
        
        recordTypeA = [SELECT Id
                       FROM RecordType
                       WHERE SObjectType = 'Contact' AND Name = 'ContactRecordTypeA' LIMIT 1].Id;	                             

    }   

    public PageReference continueNextPage() {
        PageReference newPage;

		newPage = Page.ContactRecordTypeA;
        
        if (cont.RecordTypeId != recordTypeA)
        {
            String retUrl = cont.Id;
            newPage = new PageReference('/' + retUrl);
        }
        
        if (ApexPages.currentPage().getParameters().get('retURL') != null) {
            newPage.getParameters().put('retURL', ApexPages.currentPage().getParameters().get('retURL'));
        }
               
        newPage.getParameters().put('nooverride', '1');
        
        return newPage;
    }
}

Thus, instead of making it go to the standard edit page ('/e'), we just use the record ID as the URL instead.

Hope that helps!
This was selected as the best answer
UC InnovationUC Innovation
Nkosilathi, did this work for you?
Nkosilathi NcubeNkosilathi Ncube
Sorry UC been travelling, I have just tried the second solution and it works on one Record Type it redirects but not the one that uses the visualforce page but, the URL is correct but I get the following error "An internal server error has occurred
An error has occurred while processing your request....
UC InnovationUC Innovation
Looks like you were able to resolve this since you marked a best answer?
Nkosilathi NcubeNkosilathi Ncube
Not really but its in the right direction, the error as far as I know is a salesforce issue, if you have an ideas how to get rid of the error please do, as I said the redirect URL is correct but it wont show the page