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
abhishek_kumar371abhishek_kumar371 

Unable to Change Record Type after Overriding with VF Page

I've overridden the 'Edit' button on a custom record with a Visualforce page and now I can't change the record type. It'll let me go to the 'Change' screen, click continue, but then the record type has been changed back.Please let me know the syntax and code that i need to write in controller to capture the recordtype value.....its urgent.. 
SFDC GuestSFDC Guest
Hi abhishek_kumar371,


using below vf page,
--> We can change the record type if account is already created
--> We can create select a new record type while creating an Account.
Override the new & edit buttons with below visualforce page. 

Create two record types for Account object as "Record Type 1" and "Record Type 2" before using the below code

Visualforce page:
<apex:page standardController="Account" extensions="AccountRecordTYpeCtr">
    <apex:form >
        <apex:pageBlock mode="edit" >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}"  />
            </apex:pageBlockButtons>
            <apex:messages />
            <apex:pageBlockSection title="Record Type Information" >
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Record Types" />
                        <apex:selectList value="{!Account.RecordTypeID}" multiselect="false" size="1" >
                            <apex:selectOptions value="{!RecordTypes}" />
                        </apex:selectList>
                      </apex:pageBlockSectionItem>  
                      <apex:pageBlockSectionItem rendered="{!Account.RecordTypeID!=null}">
                        <apex:outputLabel value="Record Type" />
                        <apex:outputText value="{!Account.RecordType.Name}"/>
                      </apex:pageBlockSectionItem> 
              </apex:pageBlockSection>
                        <apex:pageBlockSection title="Account Information">
                <apex:inputField value="{!Account.Name}"/>
                
                <apex:inputField value="{!Account.Type}"/>
                <apex:inputField value="{!Account.Industry}"/>
                <apex:inputField value="{!Account.AccountNumber}"/>           
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller:

public class AccountRecordTYpeCtr{
    Account accObj;
    ApexPages.StandardController controller;
    public AccountRecordTYpeCtr(ApexPages.StandardController controller) {
        this.controller = controller;
        accObj = (Account) Controller.getRecord();
    }
    public list<SelectOption> getRecordTypes() {
        list<SelectOption> options = new list<SelectOption>();
        options.add(new SelectOption('', '--None--'));
        for (list<RecordType> recordTypes : [SELECT ID, name FROM RecordType WHERE SObjectType = 'Account' ORDER BY name]) {
            for (RecordType rt : recordTypes) {
                options.add(new SelectOption(rt.ID, rt.Name));
            } 
        }
        return options;
    }
}

After clicking on Edit button: For existing record

User-added image

After clicking on new button: For New record.

User-added image

Please mark it as Best Answer if it is solved your problem.
Thank You,
Sohel Mohd