+ Start a Discussion
Varun AnnadataVarun Annadata 

displaying visual force page according to the record type selected using apex code and visual force

my requirement is this:
first i need to create two record types for a standard object lets suppose for account
then using custom setting i linked the record type and visual force page 
then using the apex code i have to display the correct visual force page according to the record type.
can anyone suggest the code for this

Hi Varun,

As DML operation is not supported with Record type, ou cannot create Record type using Apex. You have to create record type manually for any object.

You can create custom setting with Record Type Name Or Developer Name, I would suggest to use Developer Name instead of Name.

If you have two sets of pages then you can directly pass Record type ID and visualforce page (Based on Record type) on Custom Java Script button that will be operate from record detail page.

OR if you have one visualforce page, then you can fetch Record Type Name in your controller and based on Record Type you can show/hide fields or sections in your page.

I hope it helps you.


Yogesh Sharma

Chandra Sekhar CH N VChandra Sekhar CH N V
Can you explain why custom setting needs to be used explicitly? Is this a part of the requirement?
There are multiple ways for this - (you can also derive record type values in below code from a custom setting and set your conditions accordingly)
1) you can user an action method in the VF page and then in the controller use the setredirect() for navigation.
public PageReference Redirect()
     If(ApexPages.currentPage().getParameters().get('RecordType') != '<>')
           String hostname = ApexPages.currentPage().getHeaders().get('<>'); 
           String x= 'https://'+<>+'/'+'/00Q/e?nooverride=1';
           pagereference pageref = new pagereference(x);
           return pageref;
          return null;

2) You can also use render, re-render attributes in your VF pages to display fields based on record type
<apex:outputPanel rendered="{!IF(Obj.RecordTypeId!='<>',true,false)}">

3) If the display is subjected to a group/set of fields, you can go for a field set and render them in your VF page
https://developer.salesforce.com/forums/?id=906F0000000998aIAA (just a sample)
Varun AnnadataVarun Annadata
yes custom seetings is one of the requirement can u suggest code for that!!
Chandra Sekhar CH N VChandra Sekhar CH N V
Don't have an exact working code for this, but you can try the below approach and see if it works out - 

Get the recordtype values using custom setting method
String x = Objetc.getValues('<>').<yourfield>;

and then pass this string variable to PageReference redirect  -'
PageReference pageRef = new PageReference('<url>/ +x); 
Varun AnnadataVarun Annadata
String hostname = ApexPages.currentPage().getHeaders().get('<>'); using this i will get the record id from url
and by querying using soql i will get the recordname
can anyone tell how to get the vf page related to the recordname from custom setting using code?