+ Start a Discussion
cory mcory m 

Setting record type on visualforce page - creating a new record

I have a custom ojbect (Internal_Request__c) for which I am building a custom "new record" page.  Following the typical pattern of first making the user select a Record Type, I created a simple "select record type" page that gives the user a list of record types.  After submitting that page, the user is directed to the full form page with the selected Record Type ID on the query string.   Ideally, based on the new feature in version 20.0, I would like that form to be aware of the record type so that the select lists are filtered accordingly.  I can't seem to get it work though.  The page loads but the default RecordType is selected, not the one that was passed on the querystring from the previous page.

 

The parameter name on the query string is "rType"

 

Here is my controller for the second page:

 

public with sharing class CreateIRFormController {
	
    public Internal_Request__c ir {get;set;}
    
    public String rType {get; set;}
    
    public CreateIRFormController() {
        rType = ApexPages.currentPage().getParameters().get('rType');
        ir = new Internal_Request__c(recordtypeid = (ID)rType, subject__c = 'test pre-pop');
    }

    public PageReference save() {
        insert ir;
        ApexPages.StandardController sc = new ApexPages.StandardController(ir);
        return sc.edit();
    }
}

 

 

And the associated VF page:

 

<apex:page controller="CreateIRFormController">
	<apex:form >
	    <apex:pageBlock >
	        <apex:pageBlockSection >
		        <apex:inputField value="{!ir.RecordType}" />
		        <apex:inputField value="{!ir.Type__c}"  />
		        <apex:inputField value="{!ir.Account__c}"  />
		        <apex:inputField value="{!ir.Opportunity__c}"  />
		        <apex:inputField value="{!ir.Issue_Date__c}"  />
		        <apex:inputField value="{!ir.Number_of_Copies__c}"  />
		        <apex:inputField value="{!ir.Request_Due_Date_Time__c}"  />
		        <apex:inputField value="{!ir.Subject__c}"  />
		        <apex:inputField value="{!ir.Description__c}"  />
	        </apex:pageBlockSection>
	        
	       <apex:pageBlockSection >
	            <apex:inputHidden value="{!rType}" />            
	            <apex:commandButton value="Create" action="{!save}" />
	        </apex:pageBlockSection>
	
	    </apex:pageBlock>    
	</apex:form>
</apex:page>

 Note: the subject__c is correctly pre-populated by the constructor, but the record type does not work.

 

 

I tried a second version of the controller constructor, but it results in the following error message (which doesn't really make sense to me):

There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Record Type ID: value not of required type: [RecordType (Name:PS Data Center, Id:012Q00000004SJfIAM)]".

 

    public CreateIRFormController() {
        rType = ApexPages.currentPage().getParameters().get('rType');
        RecordType rt = [select id,name from RecordType where id = :rType];
        ir = new Internal_Request__c(recordtype = rt);
    }

 

 

This feels like a common thing people would want to do, so I'm hoping I'm just missing something.  Any help is appreciated.

 

-Cory

cory mcory m

UPDATE:

The form does appear to be aware of Record Type - the drop down lists are filtered correctly.  However, the RecordType select list shows the default value, not the correct one.

 

I can work around this... but still seems maybe like a bug?

BrettBBrettB

Has this been solved?

Sfdc11Sfdc11

I too facing same prob.Have u got the solution?