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
matthew.fieldmatthew.field 

Assign recordtype based on picklist value on VF page

Hi All,

I have a VF page that will be used to enter incident reports into Salesforce.  There are two recordtypes for incident reports.  I am trying to figure out how to assign the recordtype based on the value of a picklist field (if Incident_Type__c = "Moisture", recordtype = "a", "b"), but I'm not sure exactly how to spell this out on the visualforce page.

Any help will be greatly appreciated.

Thanks,
Matt
SarfarajSarfaraj
Hi Matt,

Here is one sample. Let me know if this helps.
I have a picklist field Type__c in Account. If the selected value for Type is 'Type 1'  then the account record type is 'a' and the record type is 'b' otherwise. 
Here is the simple VF,
<apex:page standardController="Account" extensions="AccountExt"> 
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Save" action="{!Save}"/>
            </apex:pageBlockButtons>
            <apex:pageBlockSection >
                <apex:inputField value="{!Account.Type__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
Here is the corresponding controller extension,
public class AccountExt {
    public PageReference Save()
    {
        RecordType rt;
        if(acc.Type__c == 'Type 1')
            rt = [Select Id From RecordType Where DeveloperName = 'a' And sObjectType = 'Account'];
        else
            rt = [Select Id From RecordType Where DeveloperName = 'b' And sObjectType = 'Account'];
        acc.RecordTypeId = rt.Id;
        upsert acc;
        return new PageReference('/' + acc.Id);
    }
    private Account acc;
    public AccountExt(ApexPages.StandardController controller) {
        acc = (Account)controller.getRecord();
    }
}
I have trimmed it down to only parts relevant to your requirement.

As you are using a VF page is have put this logic to the controller itself. However this is also possible using workflow. But workflow has a limitation in terms of querying to the RecordType object. So you need to hard code the recordtypeid in the workflow field update action.

--Akram