0
votes

I have a visualforce page that has a picklist called Topic and sometimes I will need to select one of the picklist options upon page load (meaning the Topic will be passed on from another page and will need to be selected upon loading the page for the first time). I'm not sure how to do this? I'm posting part of the Visualforce page that handles topic selection and the Controller code that below. Any help would be appreciated.Thanks.

Visualforce page:

<!---------------------------------- Select Topic ----------------------------------------------------->    
                    <apex:pageblockSection title="Select the Topic" >     
                            <apex:selectList value="{!topic}" size="1">
                                <apex:outputlabel for="Topic" value="Pick a Topic :" ></apex:outputlabel> &nbsp;&nbsp;&nbsp;
                                <apex:selectOptions id="topic" value="{!Topics}"/>
                                    <apex:actionSupport action="{!populateParameters}"  reRender="parametersSection,querySection" event="onchange"/> 
                            </apex:selectList>
                    </apex:pageblockSection>
            <!---------------------------------- End of Select Topic ---------------------------->

            <!---------------------------------- Parameters for Topic ----------------------------------------------------->    
                 <apex:pageblockSection id="parametersSection" title="Input Parameters"> 
                    <apex:repeat value="{!topicpParamWrapperList}" var="params">
                        <apex:outputPanel >
                            <apex:outputlabel value="{!params.parameter.Name}" ></apex:outputlabel> &nbsp;&nbsp;&nbsp; 
                            <apex:inputfield value="{!params.parameter.inputValue__c}" rendered="{!params.renderAsText}"> 
                                <apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
                            </apex:inputfield> 
                            <apex:inputfield value="{!params.parameter.DateTimeValueHolder__c}" rendered="{!params.renderAsDate}">
                                <apex:actionsupport action="{!placeValuesInQuery}" reRender="querySection,splunUrlLink" event="onchange"/>
                            </apex:inputfield>
                        </apex:outputPanel>
                    </apex:repeat>
                 </apex:pageblockSection>
            <!---------------------------------- End of Parameters for Topic ----------------------------------------------------->    

Apex Controller

public List < topicpParamWrapper > topicpParamWrapperList {
      get;
      set;
   } {
      topicpParamWrapperList = new List < topicpParamWrapper >();
   }


public void populateParameters() 
{
        if(!topicpParamWrapperList.isEmpty())
        {
                topicpParamWrapperList.clear();
        }

        if(topic!='' && topic!=Null)
        {
                for(Query_Parameter__c qParam :[select id, Parameters__r.Variable_Name__c, Parameters__r.Type__c,Parameters__r.Name  from Query_Parameter__c where Topics__c=:topic])
                {
                        Parameters__c param = new Parameters__c();
                        param.Name =qParam.Parameters__r.Name ;
                        param.type__c = qParam.Parameters__r.type__c;
                        param.Variable_Name__c=qParam.Parameters__r.Variable_Name__c;
                        topicpParamWrapperList.add(new topicpParamWrapper(param));
                }
                getQueryToRun();
        }

}

public void getqueryToRun(){

        if(mapTopics.containsKey(topic))
        {
                this.queryToRun =mapTopics.get(topic).query__c;
                this.queryMain=mapTopics.get(topic).query__c;
        }

} 

 public List < topicpParamWrapper > paramList {
      get;
      set;
   } {
      paramList = new List <topicpParamWrapper>();
   }
1

1 Answers

0
votes

All you really have to do is to set the topic to some initial value in the constructor (the special function that has name identical to class' name). You set it to some value and then it'll be rendered properly in visualforce (assuming same value is one of the selectable options!).

You have omitted the constructor or <apex:page> tag so we don't know how you're navigating to that page. But probably easiest for you would be to pass the topic in the URL. So if you access the page like that:

/apex/MyPage?topic=Something, something

then in the constructor you could do this:

topic = ApexPages.currentPage().getParameters().get('topic');

(the name of the URL parameter doesn't have to be same as the variable name but it makes sense to have them at least similar)

You can read more about getParameters()

If there is risk that your topic will contain &, spaces etc you probably should URLENCODE it when building the link.