2

I have a strange issue.In my form I am using select list tag with multiselect attribute=false.The problem is,selected value for this picklist which is stored in database does not comes as selected in the vf page also after selecting any value from this picklist when I click on save button the associated controller method is not called and hence the pagereference remains the current page only and not the detail page of the opportunity. Here goes my code

<apex:page standardController="Opportunity" extensions="TBN_TractOpportunityController">        
<Apex:form id="formId"> 
    <apex:pageBlock >
        <apex:repeat value="{!lstInnerOpportunity}" var="innerObj">   
            <apex:pageBlockSection columns="1" title="{!innerObj.prod.name}">    
                <apex:repeat value="{!innerObj.lstQuesAns}" var="qaObj">
                    <apex:outputField value="{!qaObj.quiz.Question__c}" /> 
                    <apex:inputTextArea value="{!qaObj.ans.Answer__c}" rendered="{!IF((qaObj.recType=='Paragraph'),'true','false')}"/>

                     <apex:selectRadio value="{!qaObj.ans.Answer__c}" rendered="{!IF((qaObj.recType=='Yes No'),'true','false')}">
                        <apex:selectOption itemValue="Yes" itemLabel="Yes" />
                        <apex:selectOption itemValue="No" itemLabel="No" />
                    </apex:selectRadio> 

                    <apex:selectList value="{!qaObj.ans.answer__c}" size="1" rendered="{!IF((qaObj.recType=='Picklist type answer'),'true','false')}">
                        <apex:selectOptions value="{!qaObj.picklistValues}"/>  
                    </apex:selectList>
                    ---{!qaObj.ans.answer__c}== 

                </apex:repeat>                      
            </apex:pageBlockSection> 
        </apex:repeat>

         <apex:pageBlockButtons >
            <apex:commandButton action="{!saveAnswer}" value="Save" />
        </apex:pageBlockButtons>             
    </apex:pageBlock>
</Apex:form>

Controller

public with sharing class TBN_TractOpportunityController 
{
    public List<OpportunityInner> lstInnerOpportunity{get;set;}
    public Map<Id,List<InnerQuestionAnswer>> mapProductToQuestionAnswer{get;set;} 
    Map<Id,set<Id>> mapGroupIdToSetQuestionId{get;set;}
    ApexPages.StandardController sc{get;set;}

/* Start Constructor */
public TBN_TractOpportunityController(ApexPages.StandardController sc) 
{
    /* Initialize variable */
    lstInnerOpportunity = new List<OpportunityInner>();
    mapProductToQuestionAnswer = new Map<Id,List<InnerQuestionAnswer>>();
    mapGroupIdToSetQuestionId = new Map<Id,Set<Id>>();


    Set<Id> setProductId = new Set<Id>();

    this.sc = sc;
    Opportunity op = (Opportunity)sc.getRecord();       
    for(OpportunityLineItem o : [SELECT id,PricebookEntry.Product2Id,OpportunityId FROM OpportunityLineItem WHERE OpportunityId =: op.id])
    {
        setProductId.add(o.PricebookEntry.Product2Id);

    }

    for(Product2 p : [SELECT id,name,Product_Group__c,Product_Group__r.id FROM Product2 WHERE id in:setProductId])
    {

        List<InnerQuestionAnswer> lstQuesAns;
        prepareMapProductToQuestionAnswer(p,op);

        //check all values added or not
        lstQuesAns = mapProductToQuestionAnswer.get(p.id);          
        if(lstQuesAns== null)
        {
            lstQuesAns = new List<InnerQuestionAnswer>();               
        }

        List<InnerQuestionAnswer> lstTempQa = new List<InnerQuestionAnswer>();
         for(Id qid : mapGroupIdToSetQuestionId.get(p.Product_Group__r.id))   
        {
            Integer i = -1;
            if(!lstQuesAns.isEmpty())
            {
                for(InnerQuestionAnswer inObj : lstQuesAns) 
                {
                    i++;
                    if(inObj.quiz.id == qid)
                    {
                        break;
                    }
                    if(i == lstQuesAns.size()-1)
                    {
                        lstTempQa.add(new InnerQuestionAnswer(qid,new Answer__c(Opportunity__c=op.id, Product__c=p.id, Question__c= qid)));

                    } 
                }
            }
            else
            {

                lstQuesAns.add(new InnerQuestionAnswer(qid,new Answer__c(Opportunity__c=op.id, Product__c=p.id, Question__c= qid))); 
            }

        }   

        if(!lstTempQa.isEmpty())
        {
            for(InnerQuestionAnswer innerQa : lstTempQa)
            {
                lstQuesAns.add(innerQa);
            }
        }   

        OpportunityInner opInner = new OpportunityInner(p,lstQuesAns);  

        lstInnerOpportunity.add(opInner); 
    }

}
/* End Constructor */

public class OpportunityInner 
{
    public Product2 prod{get;set;}
    public List<InnerQuestionAnswer> lstQuesAns{get;set;}

    OpportunityInner(Product2 prod,List<InnerQuestionAnswer> lstQuesAns)
    {
        this.prod = prod;
        this.lstQuesAns = lstQuesAns;                       
    }
}

public class InnerQuestionAnswer
{
    public String strAns{get;set;}
    public Question__c quiz{get;set;} 
    public Answer__c ans{get;set;}
    public String recType{get;set;}     

    public List<SelectOption> getPicklistValues()
    {
        List<SelectOption> options = new List<SelectOption>();
        List<String>lstListOptions = new List<String>();
        lstListOptions = String.valueOf(quiz.Picklist_value_for_question__c).split('\n');//

        for(String selectVal : lstListOptions)
        {
            options.add(new SelectOption(selectVal,selectVal));
        }          
        return options; 
    }

    public InnerQuestionAnswer(Id quizId,Answer__c ans)
    {
        quiz = [SELECT id,Question__c,RecordType.name,Picklist_value_for_question__c,Multiselect_picklist_values__c FROM Question__c where id =: quizId];  
        recType = quiz.RecordType.name;
        this.ans = ans;
        strAns = ans.answer__c;     
    }  
}

/*
    @Method Name    : prepareMapProductToQuestionAnswer
    @Parameters     : Product2,Opportunity
    @Description    : This method associate products with list of question answers
*/ 
public void prepareMapProductToQuestionAnswer(Product2 p,Opportunity op)  
{
    Set<Id> setQuizId = new Set<Id>();
    Product_Group__c pGroup = [SELECT id,Name from Product_Group__c where id = : p.Product_Group__c];
    List<Question__c> lstQuestion = [   SELECT 
                                        id,Question__c,Multiselect_picklist_values__c,
                                        Picklist_value_for_question__c,Product_group__c,
                                        recordType.name 
                                        FROM Question__c 
                                        WHERE Product_group__c =: pGroup.id 
                                    ];
    for(Question__c quiz : lstQuestion)
    {
        setQuizId.add(quiz.id);
        if(!mapGroupIdToSetQuestionId.containsKey(quiz.Product_group__c))
        {
            mapGroupIdToSetQuestionId.put(quiz.Product_group__c,new Set<Id>());             
        }
        mapGroupIdToSetQuestionId.get(quiz.Product_group__c).add(quiz.id); 
    }


    for(Answer__c ans : [SELECT id,Answer__c,Question__c,Product__c,opportunity__c from Answer__c where Question__c in:setQuizId and Opportunity__c =:op.id and Product__c =:p.id])
    {           

        if(!mapProductToQuestionAnswer.containsKey(ans.Product__c)) 
        {
            mapProductToQuestionAnswer.put(ans.Product__c,new List<InnerQuestionAnswer>());
        }
        mapProductToQuestionAnswer.get(ans.Product__c).add(new InnerQuestionAnswer(ans.Question__c,ans));           

    }

}


/*
    @Method Name    : saveAnswer
    @Parameters     : none
    @Description    : This method saves the answer
*/ 
public PageReference saveAnswer()
{
    //      public List<InnerQuestionAnswer> lstQuesAns{get;set;}

    List<Answer__c> lstAns = new List<Answer__c>();
    if(!lstInnerOpportunity.isEmpty())
    {
        for(OpportunityInner opInner : lstInnerOpportunity)
        {
            for(InnerQuestionAnswer inObj : opInner.lstQuesAns)
            {
                System.debug('------->>>> Inner Answer :'+inObj.ans);
                //
                /*if(inObj.ans.answer__c!=null && inObj.ans.opportunity__c!=null  && inObj.ans.product__c!=null) 
                {
                    lstAns.add(inObj.ans);                      
                }*/
                //isNotBlank
                if(String.isNotBlank(inObj.ans.answer__c) && String.isNotBlank(inObj.ans.opportunity__c ) && String.isNotBlank(inObj.ans.product__c)) 
                {
                    lstAns.add(inObj.ans);                      
                }

            }                   

        }
    }

    if(!lstAns.isEmpty())  
        upsert lstAns; 

    return sc.view();       
}

}

When I chose value apart from select list tag and click on save the control transfers to respective opportunity detail page and values are saved at database. But the moment I chose any value from select list tag and click save, the control does not transfers to controller and I remain on the same page and not get redirected opportunity detail page as expected. Please help

Thanks in advance

1 Answer 1

1

Couple of things to help debug this issue

1)Use apex:pagemessages tag on the page to see if any issue due to validation or any other blocker

2)For Rendered I usually prefer wrapping the field in output Panel rather than directly on the field

3
  • Actually I haven't defined any validation rule on any object. I have edited my question little bit please check it once again! Apr 18, 2015 at 15:37
  • 1
    Do you see any error in logs ?Also any special characters in your question or answer thats preventing .No harm in adding pagemessage it helps to figure cause Apr 18, 2015 at 15:53
  • 1
    I am just so thankful to you! I added the page message tag on vf page and I got the error ! It was showing validation error.Strange though! So before adding values to select option and returning list of select option to VF page I just called string class trim method to for the value to be added to selectOption like this - selectOption.add(selectVal.trim(),selectVal.trim()). and it is solved . Once again . Thanks buddy :) Apr 19, 2015 at 9:27

You must log in to answer this question.

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