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
prati@salesforceprati@salesforce 

Vf page and controller

Hi folks...
  Here is my vf page code and controller code. But I am getting an error called Unknown property 'ContactgivenId.con. I have defined getter and setter in my controller. Any help will be appreciated.. Thanks in advance..
Vf page code_______________________
<apex:page id="pgId" controller="ContactgivenId">
    <Apex:form>
        <apex:pageBlock>
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!acct}" var="account">
                    <Apex:column headervalue="Account id">
                        <Apex:outputPanel >
                            <Apex:outputText value="Click" />
                            <apex:actionSupport event="onclick" action="{!getContact}" rerender="contactBlock" >
                                <Apex:param name="accountId" assignTo="{!acctId}" value="{!account.Id}"/>
                            </apex:actionSupport>
                                   
                        </apex:outputPanel >
                    </apex:column>
                    <Apex:column headervalue="Account name">
                        <Apex:outputField value="{!account.Name}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       
        <apex:pageBlock id="contactBlock">
            <Apex:pageblockSection title="Contacts">

                <apex:pageBlockTable value="{!con}" var="contact">
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!contact.Id}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!contact.firstName}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>

        </apex:pageBlock>

    </apex:form>
</apex:page>
___________________________________________Controller________________________________
public class ContactgivenId {
    public List<Account> acct{get; set;}
    public List<Contact> cont{get; set;}
    public String acctid{get; set;}
    public ContactgivenId(){
        acct=new List<Account>();
        cont= new List<Contact>();
        acct=[select id,name from account LIMIT 10];
        
    }
    public void getContact(){
        cont=[select firstname, lastname, phone from contact where account.id= :acctid];
    }
}
Best Answer chosen by prati@salesforce
mritzimritzi
Remove your existing code and paste following codes, it will definitely work:

Apex
public class ContactgivenId {
    public List<Account> acct{get; set;}
    public List<Contact> cont{get; set;}
    public String acctid{get; set;}
    public ContactgivenId(){
        acct=new List<Account>();
        cont= new List<Contact>();
        acct=[select id,name from account LIMIT 10];
        
    }
    public void getContact(){
        cont=[select id, firstname, lastname, phone from contact where account.id= :acctid];
    }
}

VF
<apex:page id="pgId" controller="ContactgivenId">
    <Apex:form >
        <apex:pageBlock >
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!acct}" var="account">
                    <Apex:column headervalue="Account id">
                        <Apex:outputPanel >
                            <Apex:outputText value="Click" />
                            <apex:actionSupport event="onclick" action="{!getContact}" rerender="contactBlock" >
                                <Apex:param name="accountId" assignTo="{!acctId}" value="{!account.Id}"/>
                            </apex:actionSupport>
                                   
                        </apex:outputPanel>
                    </apex:column>
                    <Apex:column headervalue="Account name">
                        <Apex:outputField value="{!account.Name}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       
        <apex:pageBlock id="contactBlock">
            <Apex:pageblockSection title="Contacts">

                <apex:pageBlockTable value="{!cont}" var="c">
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!c.Id}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!c.firstName}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>

        </apex:pageBlock>

    </apex:form>
</apex:page>


Mark it as Solved, if it works.

All Answers

sslodhi87sslodhi87
<apex:pageBlockTable value="{!con}" var="contact">

​ public List<Contact> cont{get; set;}


You need to replace con with cont in first line or vise versa

 
prati@salesforceprati@salesforce
Thank you. Itried that and now I am getting a different error.. Error: Unknown property 'void.Id'.. Do I have to create  a void method for getId as well?
prati@salesforceprati@salesforce
I thought thats because I dont have id attribute in the SOQL for cont and I fixed that but still I am getting the same error.
 
mritzimritzi
@prati

use following code in your VF page and it should work fine.
There was some issue with var name -> contact
 
<apex:pageBlock id="contactBlock">
            <Apex:pageblockSection title="Contacts">

                <apex:pageBlockTable value="{!con}" var="c">
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!c.id}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!c.firstname}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>

     </apex:pageBlock>

 
prati@salesforceprati@salesforce
Thank you but I am still getting same error..
Nayana KNayana K
Change the method name getContacts() to some other name say ,fetchContacts()  and call that in actionsupport 
prati@salesforceprati@salesforce
Error: Unknown property ‘VisualforceArrayList.Id’ I am still getting the same error… Please help.
Nayana KNayana K
You dint query id for contact but using id in vf page...that's why the error...Try :

cont = [select id, firstname from contact where accountid =: acctid];
mritzimritzi
Remove your existing code and paste following codes, it will definitely work:

Apex
public class ContactgivenId {
    public List<Account> acct{get; set;}
    public List<Contact> cont{get; set;}
    public String acctid{get; set;}
    public ContactgivenId(){
        acct=new List<Account>();
        cont= new List<Contact>();
        acct=[select id,name from account LIMIT 10];
        
    }
    public void getContact(){
        cont=[select id, firstname, lastname, phone from contact where account.id= :acctid];
    }
}

VF
<apex:page id="pgId" controller="ContactgivenId">
    <Apex:form >
        <apex:pageBlock >
            <Apex:pageblockSection title="Accounts">
                <apex:pageBlockTable value="{!acct}" var="account">
                    <Apex:column headervalue="Account id">
                        <Apex:outputPanel >
                            <Apex:outputText value="Click" />
                            <apex:actionSupport event="onclick" action="{!getContact}" rerender="contactBlock" >
                                <Apex:param name="accountId" assignTo="{!acctId}" value="{!account.Id}"/>
                            </apex:actionSupport>
                                   
                        </apex:outputPanel>
                    </apex:column>
                    <Apex:column headervalue="Account name">
                        <Apex:outputField value="{!account.Name}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>
        </apex:pageBlock>
       
        <apex:pageBlock id="contactBlock">
            <Apex:pageblockSection title="Contacts">

                <apex:pageBlockTable value="{!cont}" var="c">
                    <Apex:column headervalue="Contact id">
                        <Apex:outputField value="{!c.Id}"/>
                    </apex:column>
                    <Apex:column headervalue="Contact name">
                        <Apex:outputField value="{!c.firstName}" />
                    </apex:column>
                </apex:pageBlockTable>
            </Apex:pageblockSection>

        </apex:pageBlock>

    </apex:form>
</apex:page>


Mark it as Solved, if it works.
This was selected as the best answer
prati@salesforceprati@salesforce
Thank you it worked but the rerendering part didn’t work. I am getting a table with heading as account id and account name and all the account names but when I click on click, nothing happens. So the second page block is not working.Please help me
mritzimritzi
Its working absolutely fine on my system. I m puzzled why its not working on yours.

Are you clicking on the TEXT "Click" and not somewhere else? If not, do that.

If the problem persits check manually whether there are associated contacts in your SFDC org for those accounts.

Consider sharing screenshots and your final code, if its not resolved.
prati@salesforceprati@salesforce
Thank you so much yes its working.. I had no contacts that's why  I was not able to see anything. I have a quick question though..the getContact action is in the controller as a void method, I thought it should have a return type of contacts so that it can give contact list when called, also when should i use a return type and when  a void type?
   Thanks once again..
mritzimritzi
On the fly i can think of 2 situations where a return type is used, when a method is called from VF

1. Populating a selectList/PageBlockTable/Dependent picklists
2. Redirect users to aonther page

There are few other scenarios I can't recall though.

But don't query the DB on each button click, its against SFDC best practices.
Always bulkify your SOQL query for effective use of your org limits.
Learn more about it here:
https://developer.salesforce.com/page/Apex_Code_Best_Practices