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
Gaurav GulanjkarGaurav Gulanjkar 

Record Type Picklist

Hi All,

I have a requirement where in i have to show a picklist of record types on VF page based on the logged in user.The pick list should only display the case record types that the current user has permission to create cases.When user change the value in the picklist, then change the case record type field and refresh the entire page.Need help on the approach and any sample code reference.    
Best Answer chosen by Gaurav Gulanjkar
JayantJayant
Okay, did you receive an error ?

Try <apex:actionSupport event="onchange" action="{!Save}"/> instead of following on the Page - 
<apex:actionSupport event="onchange" action="{!updateRecordType}"/>.
Save() is defined in the standard controller, you do not need to define it.


Else in Extension, if Save() did not worked, you may use this (changes on both Page and Extension to be done)- 

In extension -
 
public case currentCase {get; set;}
public Case_RemoveMessageFlag(ApexPages.StandardController stdController) {
        this.currentCase = [Select Id, RecordTypeId, SystemNote__c from Case where Id = :stdController.getRecord().Id limit 1];
    }
public PageReference updateRecordType() {
        update currentCase;
        return null;
    }


instead of - 

private final Case currentCase;
public Case_RemoveMessageFlag(ApexPages.StandardController stdController) {
        this.currentCase = [Select Id, SystemNote__c from Case where Id = :stdController.getRecord().Id limit 1];
    }
public PageReference updateRecordType() {
        this.currentCase.RecordTypeId = this.currentCase.RecordTypeId;
        update this.currentCase;
        return null;
    }

and in Page - 

<apex:inputField id="txt1" style="width:196px;" value="{!currentCase.RecordTypeId}" onchange="refreshPage()">
        <apex:actionSupport event="onchange" action="{!updateRecordType}"/>
</apex:inputField>

instead of - 

<apex:inputField id="txt1" style="width:196px;" value="{!case.RecordTypeId}" onchange="refreshPage()">
        <apex:actionSupport event="onchange" action="{!updateRecordType}"/>
        </apex:inputField>

 

All Answers

JayantJayant
Simply use an InputField tag to bind to record type. If you are using standard controller as Case, permissions will automatically be taken care of.
Have not tried this with RecordType but works with Owner for sure. Both Owner and RecordType are references to another object so should work with RecordType as well.

You may use either actionSupport or actionFunction to refresh the page. You will need to bind the method using onchange attribute of inputField.  For sample code, see VisualForce Developer's Guide. It has pretty simple and good code examples.
JayantJayant
If this is resolved, please do mark the question as Resolved and the most appropriate/helpful answer as the best answer :).
If none of the answers helped you significantly, please post the solution. You may also mark your solution as the best answer.
Gaurav GulanjkarGaurav Gulanjkar
Hi Jayant,

<apex:inputfield> worked but than i need to pass the selected recordType to the controller and update the custom field on that case.
How do i send the Id of the record type to the controller and update the case custom field.
JayantJayant
<apex:InputField> has implicit setter and would automatically reflect the changes to database, you just need to save the record/list that was being displayed on the page. Until you fire the DML, the change is stored in memory. To access the new value, you just need to access the expression that you have used to bind to the InputField component.

for e.g. if <apex:InputField value="{!c.RecordTypeId}"> is what you have used on Page, c.RecordTypeId in controller will give you the updated value that is currently selected on Page.
Gaurav GulanjkarGaurav Gulanjkar
Hi Jayant,

I tried to access the case recordTypeId in the class.But i get the recordTypeId which is already assigned to the custom field.Following the code i am trying:

Visualforce Page
<apex:page standardController="Case" extensions="Case_RemoveMessageFlag" showHeader="false" applyHtmlTag="false" applyBodyTag="false">
<script type="text/javascript">
function refreshPage()
{
    window.top.location='/console';
}
</script>
    <apex:form >
        <apex:outputText style="font-weight: bold;" value="Case Record Type"></apex:outputText>
        <br/>
        <apex:inputField id="txt1" style="width:196px;" value="{!case.RecordTypeId}" onchange="refreshPage()">
        <apex:actionSupport event="onchange" action="{!updateRecordType}"/>
        </apex:inputField>

        <br/>
        <apex:commandButton rendered="{!HasMessage}" action="{!RemoveMessageFlag}" value="Clear Flag" id="theButton" onclick="refreshPage()"/>    
    </apex:form>
</apex:page>

Controller
public class Case_RemoveMessageFlag {
    private final Case currentCase;
    public Case_RemoveMessageFlag(ApexPages.StandardController stdController) {
        this.currentCase = [Select Id, SystemNote__c from Case where Id = :stdController.getRecord().Id limit 1];
    }
    public boolean getHasMessage()
    {
        if(this.currentCase.SystemNote__c.contains('#NewMessage#') || this.currentCase.SystemNote__c.contains('#TaskCompleted#'))
            return true;
        return false;
    }
    public PageReference RemoveMessageFlag()
    {
        if(this.currentCase.SystemNote__c.contains('NewMessage#') || this.currentCase.SystemNote__c.contains('#TaskCompleted#'))
        {
            this.currentCase.SystemNote__c = '#';
            update this.currentCase;
        }
        return null;
    }
    public PageReference updateRecordType()
    {
        this.currentCase.RecordTypeId = this.currentCase.RecordTypeId;
        update this.currentCase;
        return null;
    }

}
 
JayantJayant
Remove final from - private final Case currentCase;

And try using this - 
 public PageReference updateRecordType()
    {
        Save();
        return null;
    }
Gaurav GulanjkarGaurav Gulanjkar
Hi Jayant,

There is no save() method defined.
JayantJayant
Okay, did you receive an error ?

Try <apex:actionSupport event="onchange" action="{!Save}"/> instead of following on the Page - 
<apex:actionSupport event="onchange" action="{!updateRecordType}"/>.
Save() is defined in the standard controller, you do not need to define it.


Else in Extension, if Save() did not worked, you may use this (changes on both Page and Extension to be done)- 

In extension -
 
public case currentCase {get; set;}
public Case_RemoveMessageFlag(ApexPages.StandardController stdController) {
        this.currentCase = [Select Id, RecordTypeId, SystemNote__c from Case where Id = :stdController.getRecord().Id limit 1];
    }
public PageReference updateRecordType() {
        update currentCase;
        return null;
    }


instead of - 

private final Case currentCase;
public Case_RemoveMessageFlag(ApexPages.StandardController stdController) {
        this.currentCase = [Select Id, SystemNote__c from Case where Id = :stdController.getRecord().Id limit 1];
    }
public PageReference updateRecordType() {
        this.currentCase.RecordTypeId = this.currentCase.RecordTypeId;
        update this.currentCase;
        return null;
    }

and in Page - 

<apex:inputField id="txt1" style="width:196px;" value="{!currentCase.RecordTypeId}" onchange="refreshPage()">
        <apex:actionSupport event="onchange" action="{!updateRecordType}"/>
</apex:inputField>

instead of - 

<apex:inputField id="txt1" style="width:196px;" value="{!case.RecordTypeId}" onchange="refreshPage()">
        <apex:actionSupport event="onchange" action="{!updateRecordType}"/>
        </apex:inputField>

 
This was selected as the best answer
Gaurav GulanjkarGaurav Gulanjkar
Hey Jayant the solution worked. I just called the save method in action support.
Thanks.