[SalesForce] How to perform dependent picklist operation in Visualforce page

I have a requirement to display the set of questions in VF Page. The set of questions will be available in Custom object. I need to display the question field and answer field for the user to answer. There are two type of questions will be available one is Y/N or Numbered list. For Y/N type questions we have another pick list which is having values like Yes or No. Each Yes or No having separate text boxes If the user select Yes then the yes text box (Comma separated) values will be displayed as a Pick list in VF Page else the No text box values (Comma Separated) values will be displayed as a pick list list in VF Page. How to achieve this functionality? Can anyone please help me out in this.

enter image description here

VF Page:

 <!--<apex:pageBlockSection title="Questionnaire">-->
              <apex:datatable columns="1"  cellpadding="5" cellspacing="2" value="{!questionnaire.DQAList}" var="dqa" >
              <apex:column >
                <apex:outputLabel value="{!dqa.Question}" />
              </apex:column>
               <!--<apex:column rowspan="2" rendered="{!dqa.IsGeneral}">-->
                  <apex:column rendered="{!dqa.IsGeneral}">
                <apex:inputField value="{!dqa.GeneralAnswer.Question_Master_ID__r.Answer__c}" onchange="changeAnswerReason();"/>
                <apex:selectList value="{!dqa.Answer}" size="1" id="answerlist">
                  <apex:selectOptions value="{!dqa.Options}"/>
                </apex:selectList>

             </apex:column>  
              </apex:datatable>     
              <apex:datatable columns="1" cellspacing="2" value="{!questionnaire.DQAList}" var="dqa" >
              <apex:column >
                <apex:outputLabel value="{!dqa.Question1}" />
              </apex:column>

              <apex:column rendered="{!NOT(dqa.IsGeneral)}">
                <apex:inputField value="{!dqa.DynamicAnswer.Numbered_List__c}"/>
              </apex:column> 
              </apex:datatable> 

           <!--</apex:pageBlockSection>--> 
               <apex:commandButton value="Submit" action="{!saveAnswers}" rerender="questionnairepopup"/>
               <apex:commandButton value="Save" action="{!saveAnswers}" rerender="questionnairepopup"/>
         </apex:pageBlock>
    </apex:outputPanel>
</apex:outputPanel>

Apex Page:

 public void getGeneralQuestionDetails()
    {
        Map<ID,Question__c> QuestionMap = new Map<ID,Question__c>([SELECT id, name, Question_Master_ID__r.Answer_Type__c, 
                        Question_Master_ID__r.Answer__c,Question_Master_ID__r.Question_Text__c,
                        Question_Master_ID__r.Yes_Value1__c,Question_Master_ID__r.No_Value1__c,
                        Question_Master_ID__r.Numbered_List__c 
                    FROM Question__c 
                    WHERE Customer_Category__c = :CustomerCategory 
                        AND Customer_Grading__c = :CustomerGrading]);
        system.debug('QuestionMap :' + QuestionMap);
        for( Question__c Qu : QuestionMap.values())
        {   
            DisplayQuestionAnswer DQA = new DisplayQuestionAnswer();
            DQA.IsGeneral = true;
            DQA.RecId = Qu.id;
            DQA.SNo = ++count;
            DQA.Question = Qu.Question_Master_ID__r.Question_Text__c;
            DQA.AnswerYN =  Qu.Question_Master_ID__r.Answer__c;
            DQA.Answertype = Qu.Question_Master_ID__r.Answer_Type__c;
            if(Qu.Question_Master_ID__r.Answer_Type__c == 'Y/N')
            {    
                DQA.IsAnswerYN = true;
                if(Qu.Question_Master_ID__r.Answer__c == 'Yes')
                {
                    DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.Yes_Value1__c);
                    DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.No_Value1__c);
                 }
                else if (Qu.Question_Master_ID__r.Answer__c == 'No')
                {  
                   DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.Yes_Value1__c);
                  DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.No_Value1__c); 
                }
                /*if (DQA.AnswerYN == 'Yes')
                DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.Yes_Value1__c);
                else if (DQA.AnswerYN == 'No')
                DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.No_Value1__c);*/
                /*if(Qu.Question_Master_ID__r.Answer__c == 'Yes')
                {
                    DQA.AnswerYN = Qu.Question_Master_ID__r.Answer__c;
                    DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.Yes_Value1__c);
                } 
                else if (Qu.Question_Master_ID__r.Answer__c == 'No')
                {
                    DQA.AnswerYN = Qu.Question_Master_ID__r.Answer__c;
                    DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.No_Value1__c);
                }  */     
            }
            else 
            {
                DQA.IsAnswerYN = false;
                DQA.Options = SplitCommaValues(Qu.Question_Master_ID__r.Numbered_List__c);
            }
            DQA.GeneralAnswer = Qu;
            DQAList.Add(DQA);
        }   
    }
/**** Change Answer Reason Starts ****/
public void changeAnswerReason()
{
    //getAccountDetails();
    //Questionnaire_List1(recordId);
    if(DQAList.size() > 0)
    {
        for(DisplayQuestionAnswer dqa: DQAList)
        {
            system.debug('changeAnswerReason:' + dqa);
            if(dqa.IsGeneral && dqa.Answertype == 'Y/N')
            {
                if(dqa.GeneralAnswer.Question_Master_ID__r.Answer__c == 'Yes')
                    dqa.Options = SplitCommaValues(dqa.GeneralAnswer.Question_Master_ID__r.Yes_Value1__c);
                else
                    dqa.Options = SplitCommaValues(dqa.GeneralAnswer.Question_Master_ID__r.No_Value1__c); 
            }
        }
    }
}
/**** Change Answer Reason Ends ****/

Best Answer

you can try using <apex:actionSupport> with 'onchange' to rerender the picklist depending on the answer. 'yes'/'no' will be post back to the controller then you can decide which picklist values to present base on the answer. hope this helps.

Related Topic