0

I have a picklist on VF that displays all the recordtypes list and this list is populating from apex controller.

Now I have another picklist in VF down to the record type and want to display field (TYPE picklist in object) according to the selected record ype.

The first pickList is fine its loading all the record type and I have binded the change method on that as well that is also fine and working but now how I can query the field in action method of recordtype picklist

VF section.

            <apex:pageBlockSectionItem >
                <apex:outputLabel >Transaction Type</apex:outputLabel>
                <apex:selectList value="{!RecordType}" size="1">
                    <apex:selectOptions value="{!recordTypeOptions}"/>
                    <apex:actionSupport status="loading" event="onchange" action="{!gettypeOptions}" reRender="initialForm"/>
                </apex:selectList>
            </apex:pageBlockSectionItem>

             <apex:pageBlockSectionItem >
                <apex:outputLabel id="Types">Type</apex:outputLabel>
                <apex:selectList value="{!selectedType}" size="1">
                    <apex:selectOptions value="{!typeOptions}"/>
                    <apex:actionSupport status="loading" action="{!next}" event="onchange" reRender="PBLOCK,MSGS,ADJUSTMENTCOMPONENT,form,memSecType"/>
                </apex:selectList>
            </apex:pageBlockSectionItem>

Apex Code.

//This method is loading all the records type for first picklist this is working fine
    public List<SelectOption> getRecordTypeOptions(){

        List<SelectOption> recordTypesOptions = new List<SelectOption>();
        recordTypesOptions.add(new SelectOption('','--None--'));

        //Query the transaction2 object for fetching the list of all record types        
        for(RecordType sRecordType : [SELECT Id, Name FROM RecordType WHERE sObjectType = 'club62__TXN__c'])
        {
            // Select options are added with key,value pairing
            recordTypesOptions.add(new SelectOption(sRecordType.Id, sRecordType.Name));
        }

        return recordTypesOptions;

    }


    //This method is calling on change of above picklist    
    public PageReference gettypeOptions()
    {
         //Here I want query like select type from object where recordtypeId=" RecordType"; (RecordType this populates based on the selection of user from first picklist)
        System.debug('----------------------------------'+RecordType+'----------------------------------------');


        typeOptions = new List<SelectOption>();      
        typeOptions.add(new SelectOption('','--None--'));
        typeOptions.add(new SelectOption('','--Two--'));
        typeOptions.add(new SelectOption('','--Three--'));        
        return null;
    }       

2 Answers 2

0

The values are available directly, all you need to do is refer it in your query.

The value for the selected "Type" will be available in your variable "selectedType".

Similarly the value for selected "Record Type" will be available in the variable you have binded with RecordTypeOptions.

1
  • Yes Shailesh can you help me with preparing the query. That will be a great help. and Thank you for your answer. Feb 26, 2016 at 6:12
0

OK, you are going to have issues with getter name clash

     <apex:pageBlockSectionItem >
        <apex:outputLabel >Transaction Type</apex:outputLabel>
        <apex:selectList value="{!RecordType}" size="1">
           <apex:selectOptions value="{!recordTypeOptions}"/>
             <apex:actionSupport status="loading" event="onchange" action="{!gettypeOptions}" reRender="initialForm"/>
        </apex:selectList>
    </apex:pageBlockSectionItem>

The action="{!getTypeOptions}" has to invoke a method that returns PageReference or void.

Yet in your next VF pageBlockSection

<apex:pageBlockSectionItem >
        <apex:outputLabel id="Types">Type</apex:outputLabel>
        <apex:selectList value="{!selectedType}" size="1">
            <apex:selectOptions value="{!typeOptions}"/>
            <apex:actionSupport status="loading" action="{!next}" event="onchange" reRender="PBLOCK,MSGS,ADJUSTMENTCOMPONENT,form,memSecType"/>
        </apex:selectList>

You use <apex:selectOptions value="{!typeOptions}" that is going to look for a getter on an Apex property typeOptions or a method called getTypeOptions that needs to return a list of SelectOptions.

You already have an actionmethod for the first pageBlockSection by this name.

So, what to do

1. Change the first pageBlock actionSupport to be:

<apex:actionSupport status="loading" event="onchange" action="{!nop}" reRender="initialForm"/>

and have the corresponding actionMethod be:

public void nop() {}

technically, I don't think you need an action= at all on this actionSupport as the rerender should cause the AJAX refresh but I sometimes get tripped up here. If nothing else, the nop method lets you debug that the ajax rerender is happening.

2. Change your getTypeOptions method to:

    public SelectOption[] getTypeOptions() {
      // build the list of selectOptions by querying using the value of `this.recordType`
    }

UPDATE OP wants to display dropdowns based on the metdata-configured picklist entries for some Field Foo for RecordType A vs B, based on user selection

Normally, a VF inputField honors the object's recordType when displaying a picklist. But, you seem to be doing a bespoke solution and attempting to rebuild VF functionality by hand. So ...

Option 1: Use the Apex Metadata API wrapper to do this. Read the Apex Metadata API doc and the excellent code samples in the package to traverse the relationships you need to get the picklists

Option 2 There is a javascript hack described here using describeLayout()

As an aside, recordType would be better named as selectedRecordTypeId to make it clear that is what the user chose

3
  • crop1645 thank you for your answer I googled a lot but did not found the related query which you are suggesting. so please can you help with the query, I want query something like IF user will select the RecordType called A than I want to populate select options according to A only which I set up in A recordType in Object. If user will select B than the option should be prepared according to RecordType B setup from object and so on Feb 26, 2016 at 6:06
  • ah -- that is a different problem than what I understood. So, if Object Foo has two recordtypes A and B, you want the picklist entries configured in the metadata for Field Bar : recordType A to appear in the dropdown if user selected A, and the picklist entries for Field Bar : recordType B in the dropdown if the user selected B. This question has been asked before. I'll see if I can find that
    – cropredy
    Feb 26, 2016 at 6:17
  • @user3446467 - posted update
    – cropredy
    Feb 26, 2016 at 6:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .