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
Oleg LavvrynenkoOleg Lavvrynenko 

problem with selectList and table rerendering

I have such Visualforce code
<apex:page controller="MyController">
       <apex:PageBlock title="My Page Block">
      <apex:form >
              <apex:selectList value="{!Consultant}" multiselect="true">
                    <apex:actionSupport event="onchange" 
                    reRender="TaskBlock"/>
                  <apex:selectOptions value="{!Consultants}"></apex:selectOptions>
              </apex:selectList>
              </apex:form>
       </apex:PageBlock>
       <apex:form >
<apex:pageBlock title="Our Products" id="TaskBlock" >
  <apex:pageBlockSection columns="1">
     <apex:pageBlockTable value="{!tasks}" var="pitem" >
           <apex:column headerValue="Task Name">
                  <apex:outputText value="{!pitem.name}"/>
           </apex:column>
         <apex:column headerValue="Consultant">
             <apex:outputField value="{!pitem.Consultant__c}"/>
         </apex:column>
      </apex:pageBlockTable>
    </apex:pageBlockSection>
   </apex:pageBlock>
</apex:form>
    </apex:page>

After selection  some option, relate tasks should  appear in table.
butt it doesn't work with such controller
 
public class MyController {

 
    public String Consultant { get; set; }
     public String Consultants { get; set; }
    public List<task__c> consultList{ get; set; }
    public String[] names { get; set; }
    public List<Task__c> pitem;
public MyController()
{

}
public void setConsultant(Id value) {
 pitem = [Select name,consultant__c from task__c WHERE 
consultant__c in (SELECT id FROM contact WHERE name= 'Kristy Doherty') ];
    System.Debug('In 1'); }
 


//public void setConsultants(){}

public  Set<SelectOption> getConsultants() {
         consultList = [Select consultant__c,consultant__r.name from task__c];
        Set<SelectOption> options = new Set<SelectOption>();
        System.debug(consultList.size());
         for(integer i=0;i<consultList.size();i++)
         {
         if (consultList[i].consultant__r.name != null)
         options.add(new SelectOption(consultList[i].consultant__r.name , consultList[i].consultant__r.name  ));
         }         
         return options;        
   }

public List<task__c> getTasks() {return pitem;}
public void setTasks(Id value) {

        pitem = [Select name,consultant__c from task__c WHERE 
consultant__c in (SELECT id FROM contact WHERE name=: value) ];
                System.Debug(value);  
                 
   }
   }
How can I fix it?


 
Best Answer chosen by Oleg Lavvrynenko
Neetu_BansalNeetu_Bansal
Hi Oleg,

There are various flaws in your code: Use the below code.
Visualforce Page:
<apex:page controller="MyController">
	<apex:form >
		<apex:PageBlock title="My Page Block">
            <apex:selectList value="{!Consultant}" multiselect="true">
                <apex:actionSupport event="onchange" reRender="TaskBlock" action="{!retriveTasks}"/>
                <apex:selectOptions value="{!Consultants}"></apex:selectOptions>
            </apex:selectList
        </apex:PageBlock>
		
		<apex:pageBlock title="Our Products" id="TaskBlock" >
			<apex:pageBlockSection columns="1">
				<apex:pageBlockTable value="{!tasks}" var="pitem" >
					<apex:column headerValue="Task Name">
						<apex:outputText value="{!pitem.name}"/>
					</apex:column>
					<apex:column headerValue="Consultant">
						<apex:outputField value="{!pitem.Consultant__c}"/>
					</apex:column>
				</apex:pageBlockTable>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
Apex Class:
public class MyController
{
	public String Consultant { get; set; }
    public List<Task__c> tasks;

	public List<SelectOption> getConsultants()
	{
        List<SelectOption> options = new List<SelectOption>();
        
		for( Task__c t : [ Select consultant__c,consultant__r.name from task__c ])
        {
			if( t.consultant__c != null && t.consultant__r.name != null )
				options.add( new SelectOption( t.consultant__r.name , tconsultant__r.name  ));
        }         
        
		return options;
	}

	public void retriveTasks()
	{
		tasks = [ Select name, consultant__c from task__c WHERE consultant__c in (SELECT id FROM contact WHERE name=: Consultant )];
	}
}
Let me know, if you need any other help.

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi Oleg,

There are various flaws in your code: Use the below code.
Visualforce Page:
<apex:page controller="MyController">
	<apex:form >
		<apex:PageBlock title="My Page Block">
            <apex:selectList value="{!Consultant}" multiselect="true">
                <apex:actionSupport event="onchange" reRender="TaskBlock" action="{!retriveTasks}"/>
                <apex:selectOptions value="{!Consultants}"></apex:selectOptions>
            </apex:selectList
        </apex:PageBlock>
		
		<apex:pageBlock title="Our Products" id="TaskBlock" >
			<apex:pageBlockSection columns="1">
				<apex:pageBlockTable value="{!tasks}" var="pitem" >
					<apex:column headerValue="Task Name">
						<apex:outputText value="{!pitem.name}"/>
					</apex:column>
					<apex:column headerValue="Consultant">
						<apex:outputField value="{!pitem.Consultant__c}"/>
					</apex:column>
				</apex:pageBlockTable>
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
Apex Class:
public class MyController
{
	public String Consultant { get; set; }
    public List<Task__c> tasks;

	public List<SelectOption> getConsultants()
	{
        List<SelectOption> options = new List<SelectOption>();
        
		for( Task__c t : [ Select consultant__c,consultant__r.name from task__c ])
        {
			if( t.consultant__c != null && t.consultant__r.name != null )
				options.add( new SelectOption( t.consultant__r.name , tconsultant__r.name  ));
        }         
        
		return options;
	}

	public void retriveTasks()
	{
		tasks = [ Select name, consultant__c from task__c WHERE consultant__c in (SELECT id FROM contact WHERE name=: Consultant )];
	}
}
Let me know, if you need any other help.

Thanks,
Neetu
This was selected as the best answer
Oleg LavvrynenkoOleg Lavvrynenko
Neetu, thank you for help,  but I got an error, after clicked Preview:
Unknown property 'void.name'
Error is in expression '{!pitem.name}' in component <apex:outputText> in page timeentrypage

 
Neetu_BansalNeetu_Bansal
Hi Oleg,

This should work. Can you provide your salesforce org login credentials so I can have a look. You can contact me either on my gmail id: neetu.bansal.5@gmail.com or Skype id: neetu.bansal.5

Thanks,
Neetu
Oleg LavvrynenkoOleg Lavvrynenko
Thanks, after I changed multiselect from true  for false, it works as I expected!