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
NewBee21NewBee21 

Lookup field for Account and getting all the contacts related to that Account using extension controller

I am getting this error while previewing visualforce page. ==> Error: Unknown property 'AccountStandardController.con'


Visualforce page:

<apex:page standardController="Account" extensions="ExtensionController5">
<apex:form >
<apex:pageBlock title="Account Information" id="account"> <apex:pageBlocksection>
<apex:inputField value="{!con.accountId}"/>
<apex:actionsupport event="onchange" action="{!accountselected}" rerender="account,msgs" status="stat"/>
</apex:pageBlockSection
</apex:pageBlock>
</apex:form>
</apex:page>


Controller:

public class ExtensionController5{ private ApexPages.StandardController Stdctrl;
public ExtensionController5(ApexPages.StandardController controller){ Stdctrl=controller; }
public void AccountSelected() { contact con=(contact) StdCtrl.getRecord();
if (!string.IsBlank(con.accountid))
{ con.account=[select Name,Phone from Account where id=:con.accountid]; }
else { con.account=null; } } }
Best Answer chosen by NewBee21
Suraj Tripathi 47Suraj Tripathi 47
Hello NewBee,
Please consider the code below, this will give you output as per your requirement -
Apex Controller -

public with sharing class DisplayAccWithContact {
    public String accName {get;set;}

    List<Account> lstacc = new List<Account>();
    List<contact> lstcon = new List<contact>();
    public List<contact> getContacts() {
       lstcon.clear();
       accIds.clear();
       lstacc.clear();      
       lstacc=[select id,name from Account where name=:accName];
       for(Integer i=0;i<lstacc.size();i++)
       {
           accIds.add(lstacc[i].Id);
       }
       
        lstcon =[select id,name,Phone, email,accountId from contact where accountid in : accIds];
        //system.debug('### List of Contacts for Test is ###'+ lstcon);
        return lstcon;
    }
    
    set<string> accIds = new set<string>();
    public pagereference showContacts() {
       return null;        
    }
}

Vf Page -

<apex:page sidebar="false" controller="DisplayAccWithContact">
    <apex:form >      
        <apex:outputText value="Enter Account Name:"></apex:outputText>
        <apex:inputtext value="{!accName}" />
        <apex:commandButton value="ShowContacts" action="{!showContacts}" rerender="out" status="mystatus"/><br/>
        
        <apex:actionstatus id="mystatus" starttext="please wait contacts are loading.......">
            <apex:facet name="stop">
                <apex:outputpanel id="out">
                    <apex:pageBlock >
                        <apex:pageBlockSection >
                            <apex:pageBlockTable value="{!Contacts}" var="c" rendered="{!accName !=null}">
                                <apex:column headerValue="Name">
                                    {!c.Name}
                                </apex:column>
                                <apex:column headerValue="Phone">
                                    {!c.Phone}
                                </apex:column>
                                <apex:column headerValue="Email">
                                    {!c.Email}
                                </apex:column>
                            </apex:pageBlockTable>
                        </apex:pageBlockSection>
                    </apex:pageBlock>
                </apex:outputpanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:form>
</apex:page>


Output -
Enter Account name in search box then click on show Contacts button, then related contacts will be displayed.

*****SCREENSHOT******

 

User-added image

Thank you!

All Answers

Suraj Tripathi 47Suraj Tripathi 47
Hey NewBee,
Please consider given code below -
Apex Class -

public class DisplayContactOnAccountSelController {

    public list<AccountContactWrapper> lstAccountContactWrapper { get; set; }
    public list<AccountContactWrapper> lstSelectedAccountContactWrapper { get; set; }
    public list<account> selectedAccounts{get;set;}   

    public DisplayContactOnAccountSelController () {
        lstSelectedAccountContactWrapper = new list<AccountContactWrapper>();
        if(lstAccountContactWrapper == null) {
            lstAccountContactWrapper = new list<AccountContactWrapper>();
            for(account a:[select id,name,(select id,name from contacts) from account limit 10]) {
                lstAccountContactWrapper.add(new AccountContactWrapper(a));
            }
        }
    }
 
    public void ProcessSelected() {
        lstSelectedAccountContactWrapper =new list<AccountContactWrapper>();
        selectedAccounts =new list<Account>();
        for(AccountContactWrapper wrapobj:lstAccountContactWrapper){
            if(wrapobj.isSelected==true) {
                selectedAccounts.add(wrapobj.acc);
            } 
        }
       
        for(Account acc:[select id,name,(select id,name from contacts) from account where id in:selectedAccounts]) {
            lstSelectedAccountContactWrapper.add(new AccountContactWrapper(acc)); 
        }
             
    }
    public class AccountContactWrapper {
 
        public Account acc {get;set;}
        public boolean isSelected {get;set;}
   
        public AccountContactWrapper(account a) {
            acc = a;
            isselected=false;
        }
    }
}

Visualforce Page -
<apex:page sidebar="false" controller="DisplayContactOnAccountSelController">
<apex:form >
    <apex:pageBlock id="AccountContactBlock">
        <apex:pageBlockSection columns="2">
        <apex:pageBlockTable value="{!lstAccountContactWrapper}" var="acc" id="AccountSection">
            <apex:column >
                <apex:inputCheckbox value="{!acc.isSelected}" id="InputId"/>
            </apex:column>
            <apex:column value="{!acc.acc.name}"/>
        </apex:pageBlockTable>
        <apex:pageBlockTable value="{!lstSelectedAccountContactWrapper}" var="acc" id="contactsSection" rendered="{!IF(lstSelectedAccountContactWrapper.size > 0,true,false)}">>
            <apex:column headerValue="Account and Its Related Contacts">
                <apex:pageBlockTable value="{!acc.acc.contacts}" var="con">
                    <apex:facet name="header">{!acc.acc.Name}</apex:facet>
                    <apex:column value="{!con.name}" headerValue="Contact Name"/>
                </apex:pageBlockTable>
            </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlockSection>
        <apex:pageBlockButtons location="bottom">
          <apex:commandButton action="{!ProcessSelected}" value="Show Account and its Contacts" reRender="AccountContactBlock,contactsSection"/>
        </apex:pageBlockButtons>
    </apex:pageBlock>
</apex:form>

</apex:page>

Here is the output
Please mark it as the best answer if it helps.
Thank you!
NewBee21NewBee21
hello Suraj,thanks for the answer.And here you have created a list for account here.And from that one can fetch contacts of the account.

My requirement is I want the contacts of the Account which I search with the look up (see screenshot) and then I will get contacts related to the account to me.

heres what i achieved till the lookup part,but now stuck with how do i get contacts fetched to me of the account.

**controller**

public class freshcontroller1
{
public contact con {get;set;}
Account ac;

  public freshcontroller1(ApexPages.Standardcontroller controller)
 {
 con =new contact();
 }
public Account getAc()
{
return ac;
}
 
  public pagereference showcontact()
  {
  ac = [select id from Account where id=:con.accountid];
  System.debug('Ac  '+ac);
  return null;
  }  
  public pagereference Contact()
  {
  con=[select id,Name,Phone from Contact where id=:con.accountid];
  System.debug('contacts are '+con);
  return null;
  }
  
}


*VF Page**

<apex:page standardController="Account" extensions="freshcontroller1">
<apex:form > <apex:pageBlock title="Account information"/>
<apex:inputField value="{!con.accountid}"/>
<apex:commandButton action="{!showcontact}" value="Display contacts"/>
<apex:pageBlock > <apex:pageBlockTable value="{!con}" var="c"/>
</apex:pageBlock>
</apex:form>
</apex:page>
 
Suraj Tripathi 47Suraj Tripathi 47
Hello NewBee,
Please consider the code below, this will give you output as per your requirement -
Apex Controller -

public with sharing class DisplayAccWithContact {
    public String accName {get;set;}

    List<Account> lstacc = new List<Account>();
    List<contact> lstcon = new List<contact>();
    public List<contact> getContacts() {
       lstcon.clear();
       accIds.clear();
       lstacc.clear();      
       lstacc=[select id,name from Account where name=:accName];
       for(Integer i=0;i<lstacc.size();i++)
       {
           accIds.add(lstacc[i].Id);
       }
       
        lstcon =[select id,name,Phone, email,accountId from contact where accountid in : accIds];
        //system.debug('### List of Contacts for Test is ###'+ lstcon);
        return lstcon;
    }
    
    set<string> accIds = new set<string>();
    public pagereference showContacts() {
       return null;        
    }
}

Vf Page -

<apex:page sidebar="false" controller="DisplayAccWithContact">
    <apex:form >      
        <apex:outputText value="Enter Account Name:"></apex:outputText>
        <apex:inputtext value="{!accName}" />
        <apex:commandButton value="ShowContacts" action="{!showContacts}" rerender="out" status="mystatus"/><br/>
        
        <apex:actionstatus id="mystatus" starttext="please wait contacts are loading.......">
            <apex:facet name="stop">
                <apex:outputpanel id="out">
                    <apex:pageBlock >
                        <apex:pageBlockSection >
                            <apex:pageBlockTable value="{!Contacts}" var="c" rendered="{!accName !=null}">
                                <apex:column headerValue="Name">
                                    {!c.Name}
                                </apex:column>
                                <apex:column headerValue="Phone">
                                    {!c.Phone}
                                </apex:column>
                                <apex:column headerValue="Email">
                                    {!c.Email}
                                </apex:column>
                            </apex:pageBlockTable>
                        </apex:pageBlockSection>
                    </apex:pageBlock>
                </apex:outputpanel>
            </apex:facet>
        </apex:actionstatus>
    </apex:form>
</apex:page>


Output -
Enter Account name in search box then click on show Contacts button, then related contacts will be displayed.

*****SCREENSHOT******

 

User-added image

Thank you!

This was selected as the best answer
NewBee21NewBee21
Hi Suraj,thanks for replying,Appreciate it.

But I want to search for account using  search option (please refer screenshot) and once I select Account,then I want contacts to be shown.

You correctly solved my query about contacts fetching parts,no doubt.But I just want,Account to be searched via search option instead manually entering Account.

screenshot:User-added image