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
Tommie Thompson 24Tommie Thompson 24 

How can I hard code the record type in a VF page using a standard controller?

I have this code for a VF data entry page for a specific record type for a custom object.
<apex:page standardController="Internal_Escalation__c">
       <apex:form >
       <apex:pageBlock title="Enter Knowledge Request">
        <apex:pageBlockSection columns="1">
               <apex:inputField value="{! Internal_Escalation__C.RecordTypeId }"/>
               <apex:inputField value="{! Internal_Escalation__C.Name }"/>
                <apex:inputField value="{! Internal_Escalation__c.knowledge_type__c }"/>
                <apex:inputField value="{! Internal_escalation__c.product_area__c }"/>
                <apex:inputField value="{! Internal_escalation__c.product_feature__c }"/>
               <apex:inputField value="{! Internal_escalation__c.article_name__c }"/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
              <apex:commandButton action="{! save }" value="Save" />
              <apex:commandButton action="{! Cancel }" value="Cancel" />
        </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
</apex:page>


Instead of having the user select the record type using
 <apex:inputField value="{! Internal_Escalation__C.RecordTypeId }"/>,
I just want to hard code the record type to be the id of the Knowledge Request record type for the Internal_Escalation__C object.  This page is being called from a button called Create New Knowledge Request so it is redundant to have the user pick the record type.  
I am not a coder. I modified this code I found.  The code works for my purpose, except the record type is coming from the user picklist. Now I want to have the syntax to hard code the record type instead of having the user enter it.
I'm sure there must be a way to simply assign the record type id instead of using apex:inputfield.  
Any help will be greatly appreciated.
Abhishek BansalAbhishek Bansal
Hi Tommie,

You need a controller class for thi VF page in order to assign a default record type to this new record. Please find the modified VF page code and new extension class below:
VF Page:
<apex:page standardController="Internal_Escalation__c" extensions="EXT_InternalEscalation">
       <apex:form >
       <apex:pageBlock title="Enter Knowledge Request">
        <apex:pageBlockSection columns="1">
               <apex:outputField value="{! currentEscalationRecord.RecordTypeId }"/>
               <apex:inputField value="{! currentEscalationRecord.Name }"/>
                <apex:inputField value="{! currentEscalationRecord.knowledge_type__c }"/>
                <apex:inputField value="{! currentEscalationRecord.product_area__c }"/>
                <apex:inputField value="{! currentEscalationRecord.product_feature__c }"/>
               <apex:inputField value="{! currentEscalationRecord.article_name__c }"/>
        </apex:pageBlockSection>
        <apex:pageBlockButtons >
              <apex:commandButton action="{! save }" value="Save"/>
              <apex:commandButton action="{! Cancel }" value="Cancel" />
        </apex:pageBlockButtons>
    </apex:pageBlock>
    </apex:form>
</apex:page>

New Extension Class:
public class EXT_InternalEscalation {

	public Internal_Escalation__C currentEscalationRecord {get;set;}
	
	public EXT_InternalEscalation(ApexPages.StandardController con) {
        currentEscalationRecord = (Opportunity)con.getRecord();
		//use the record type name in place of Knowledge_Request
        currentEscalationRecord.RecordTypeId = Schema.SObjectType.Internal_Escalation__C.getRecordTypeInfosByName().get('Knowledge_Request').getRecordTypeId();
        
        init();
    }
	
	public PageReference save() {
		insert currentEscalationRecord;
		return new PageReference('/'+currentEscalationRecord.Id);
	}
}
Please take care of any syntax errors and let me know if there is any issue.

Thanks,
Abhishek Bansal.​​​​​​​
Tommie Thompson 24Tommie Thompson 24
Hello Abhhishek!

Thank you very much for being willing to help me.

I created an apex class with the Public Class code you provided.  I used Knowledge_Request for my record type name.  That is the real name.
I substituted my custom object name where you had Opportunity in the 4th line.
My extension now looks like:

public class EXT_InternalEscalation {

    public Internal_Escalation__C currentEscalationRecord {get;set;}
    
    public EXT_InternalEscalation(ApexPages.StandardController con) {
        currentEscalationRecord = (Internal_Escalation__c) con.getRecord();
        //use the record type name in place of Knowledge_Request
        //Knowledge_Request is the correct name of the record type
        //Replaced Opportunity in with Internal_Escalation__c, the name of my custom object
        currentEscalationRecord.RecordTypeId = Schema.SObjectType.Internal_Escalation__C.getRecordTypeInfosByName().get('Knowledge_Request').getRecordTypeId();
        
        init();
    }
    
    public PageReference save() {
        insert currentEscalationRecord;
        return new PageReference('/'+currentEscalationRecord.Id);
    }
}

I am getting an error :

Method does not exist or incorrect signature: void init() from the type EXT_InternalEscalation
Abhishek BansalAbhishek Bansal
Hi Tommie,

Please remove this line. It was copy and paste mistake :P

Thanks,
Abhishek Bansal.
Tommie Thompson 24Tommie Thompson 24
Hello Abhishek,

I had to stop working on this project for a bit.  I finally got back to it.  I was able to save the VF page and the Extension.  I did a preview from the developer console and was able to create a Knowledge Request as desired!!  Thank you. 

And notes for the code above:
  • I changed Opportunity to Internal_Escalation__c
  • I removed Init()
  • I used the record type Label,'Knowledge Request' (not the record type name)  in the extension
Then I tried to reference the VF page in a detail page button and I got this error:

SObject row was retrieved via SOQL without querying the requested field: Internal_Escalation__c.Name

Do I need to do something special when I call the VF Page from a button?

I will greatly appreciate any feedback.  I feel like I am very close to having a working solution.

Thanks!