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
LeadforceLeadforce 

How can I override Record Type Page Layout for a Visualforce Page??

I'm making a simple Visualforce page to be used for a button on an Account detail.  The purpose of this page is to override the limited page layout of the account Record type and open a detailed Account view with all fields, not the limited page layout determined by record type.  
This is the script I am using;
<apex:page standardController="Account">
<b> Hello {!$User.FirstName}! </b> <br/> <br/>
 <apex:detail />
</apex:page>
When I use this, the same page layout is displaying as with the Record type of this Account record.  I want a different, detailed or master page layout to be displayed when the Visualforce page is opened.  Is this Possible? Thank YOU for any input!!
Best Answer chosen by Leadforce
Suneel#8Suneel#8
We configure different page layouts for different record types.So you might need to change the record type on load of the page accordingly page layout changes.Below code might help

VF page code
<apex:page standardController="Account" extensions="LoadRecordType" action="{!setRecordType}">
    <apex:detail subject="{!account}"/>
</apex:page>

Apex controller code
 
public class LoadRecordType {

    Account a;
    
    public LoadRecordType(ApexPages.StandardController controller) {
        a=(Account)controller.getRecord();
    }
    
    public PageReference setRecordType(){
        a.RecordTypeId=[Select Id,SobjectType,Name From RecordType where Name ='YOUR_RECORD_TYPE_NAME' and SobjectType ='Account'  limit 1].id;
        update a;
        return null;
    }
}

 

All Answers

Suneel#8Suneel#8
We configure different page layouts for different record types.So you might need to change the record type on load of the page accordingly page layout changes.Below code might help

VF page code
<apex:page standardController="Account" extensions="LoadRecordType" action="{!setRecordType}">
    <apex:detail subject="{!account}"/>
</apex:page>

Apex controller code
 
public class LoadRecordType {

    Account a;
    
    public LoadRecordType(ApexPages.StandardController controller) {
        a=(Account)controller.getRecord();
    }
    
    public PageReference setRecordType(){
        a.RecordTypeId=[Select Id,SobjectType,Name From RecordType where Name ='YOUR_RECORD_TYPE_NAME' and SobjectType ='Account'  limit 1].id;
        update a;
        return null;
    }
}

 
This was selected as the best answer
LeadforceLeadforce
If I try to use this as a Apex Class,


public class LoadRecordType {
Account a;
public LoadRecordType(ApexPages.StandardController controller) { a=(Account)controller.getRecord();
}
public PageReference setRecordType(){
a.RecordTypeId=[Select Id,SobjectType,Name From RecordType where Name ='VCHospital' and SobjectType ='Account' limit 1].id; update a; return null;
}
}

And reference this as extension in Account Visualforce page,  I get this error.

System.QueryException: List has no rows for assignment to SObject
Error is in expression '{!setRecordType}' in component <apex:page> in page accountdetailbutton: Class.LoadRecordType.setRecordType: line 10, column 1
Class.LoadRecordType.setRecordType: line 10, column 1


Im kind of a visualforce Newb.  Can you help me figure out why this is happening?
Suneel#8Suneel#8
It says that below SOQL is not fetching any record.Run it in workbench and check it
Select Id,SobjectType,Name From RecordType where Name ='VCHospital' and SobjectType ='Account' limit 1