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
DevSFDevSF 

Redirect visualforce page based on record type.

Hi friends,

My requirement is to redirect my page to a visualforce page when i select a particular record type. and it should autopopulate some fields from its parent object.

Example: My visualforce page is on Contact object and after i choose a record type it should redirect to a new visualforce page with address fields on contact object auto populated from Account object.

I have tried working on this with the help of some Blog's.. But it didn't worked the way i want.

So can anyone suggest the exact way to get this requirement done (Or) any sample code would be really helpful/ appriciated.


Thanks,
Dev.
Amit Chaudhary 8Amit Chaudhary 8
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;
}

Please check below post for same issue
1) http://salesforce.stackexchange.com/questions/121142/redirect-to-visualforce-page-after-record-type-chosen

Let us know if this will help you


 
DevSFDevSF
Hi Amit,
Many thanks for your response.

Tried using above code and i have overridden the standard new button, but it is redirecting to same visualforce page for all record types, even i have specified the record typeId in my controller.
My requirement is to redirect it to visualforce page for 1 record type && for others it should stick with standard layout.

Below is the code, Please update if anything wrong.

VF PAGE:


<apex:page standardcontroller="Contact" extensions="Redirect"  action="{!getredir}">>
</apex:page>



CONTROLLER:

public class Redirect {

    public Redirect(ApexPages.StandardController controller) {
   //this.c = (Contact)myController.getRecord();

    }
public PageReference getRedir() {

     PageReference newPage;

        if (ApexPages.currentPage().getParameters().get('RecordType') == '012280000015D4W') {
            newPage = Page.Contact_New;
            return newPage.setRedirect(true);
        } else {
            return null;
        }

    }

    private final ApexPages.StandardController controller;
}


Thanks,
Dev.