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
Dilip KrDilip Kr 

get all contacts of an account onclick on count of contact of an account

User-added image
Hi all,
please let me know,how to get all contacts of an account on click on count of contact as show above screenshort.
please let me know what i missing in below code

apex class...........

public with sharing class sampleCode {

public List<wrapper> wrapperList{get; set;}
public List<Contact> contactsInformation { get; set; }
public Id selectedCountOfContact { get; set; }


public sampleCode(){
         wrapperList=new List<Wrapper>();
         for(Account a:[select Name,(select id,name from contacts),(select id from opportunities)  from Account limit 5])
         {
           Wrapper w1=new Wrapper();
           w1.Name=a.Name;
           w1.Con=a.contacts.size();
              
           w1.Opp=a.opportunities.size();
           wrapperList.add(w1);
         }       
}

class Wrapper{
    public String Name{get; set;}
    public Integer Con{get; set;}
    public Integer Opp{get; set;}   
 }
 
public List<Account> getMyAccounts() {
return [SELECT Id, Name,Industry, AccountNumber FROM Account ORDER BY
LastModifiedDate DESC LIMIT 10];
}

public void accountClicked() {
contactsInformation = [SELECT FirstName, LastName FROM Contact
WHERE AccountID = :selectedCountOfContact];
  }
}


and page.........



<apex:page controller="sampleCode">

<apex:form >

<apex:outputPanel id="ContactDetail">

<apex:repeat value="{!contactsInformation}" var="contact">
<p>{! contact.FirstName & ' ' & contact.LastName}</p>
</apex:repeat>
</apex:outputPanel>

<apex:pageBlock >
     <apex:pageBlockTable value="{!wrapperList}" var="b" >
                         <apex:column value="{!b.Name}" headerValue="Account Name"/>
                   
                         <apex:column headerValue="Total Contacts">  
                             <apex:commandlink action="{!accountClicked}" rerender="ContactDetail">
                             <apex:outputText value="{! b.con}"/>
                             <!--  <apex:param name="id" value="{!b.id}" assignTo="{!selectedcountofcontact}"/>-->
                         </apex:commandLink>                             
                         </apex:column>
                         
                         <apex:column headerValue="Total Opportunities">
                             <apex:commandLink value="{!b.Opp}"/>
                         </apex:column>                     

    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>
 
Best Answer chosen by Dilip Kr
Ravikant kediaRavikant kedia
Hi dilip kr,
                 I solved your problem use belw code and you can modify below code as your requirement. I change something in your code .

//page code
<apex:page controller="sampleCode">

<apex:form >

<apex:outputPanel id="ContactDetail">

<apex:repeat value="{!contactsInformation}" var="contact">
<p>{! contact.FirstName & ' ' & contact.LastName}</p>
</apex:repeat>
</apex:outputPanel>

<apex:pageBlock >
     <apex:pageBlockTable value="{!wrapperList}" var="b" >
                         <apex:column value="{!b.Name}" headerValue="Account Name"/>
                   
                         <apex:column headerValue="Total Contacts">  
                             <apex:commandlink action="{!accountClicked}" rerender="ContactDetail">
                             <apex:outputText value="{! b.con}"/>
                               <apex:param name="id" value="{!b.recordid}" assignTo="{!selectedcountofcontact}"/>
                         </apex:commandLink>                             
                         </apex:column>
                         
                         <apex:column headerValue="Total Opportunities">
                             <apex:commandLink value="{!b.Opp}"/>
                         </apex:column>                     

    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>



//Apex code


public with sharing class sampleCode {

public List<wrapper> wrapperList{get; set;}
public List<Contact> contactsInformation { get; set; }
public Id selectedCountOfContact { get; set; }


public sampleCode(){
         wrapperList=new List<Wrapper>();
         for(Account a:[select id,Name,(select id,name from contacts),(select id from opportunities)  from Account limit 5])
         {
           Wrapper w1=new Wrapper();
           w1.Name=a.Name;
           w1.Con=a.contacts.size();
              
           w1.Opp=a.opportunities.size();
           w1.recordid =a.id;
           wrapperList.add(w1);
         }       
}

class Wrapper{
    public String Name{get; set;}
    public Integer Con{get; set;}
    public Integer Opp{get; set;}   
    public Id recordid{get; set;}
 }
 
public List<Account> getMyAccounts() {
return [SELECT Id, Name,Industry, AccountNumber FROM Account ORDER BY
LastModifiedDate DESC LIMIT 10];
}

public void accountClicked() {
contactsInformation = [SELECT FirstName, LastName FROM Contact
WHERE AccountID = :selectedCountOfContact];
  }
}
I checked it and now it's working as your requirement and select best answer if it solve your problem .

All Answers

Ravikant kediaRavikant kedia
I check your code your code output is same as in above screenshot and i don't  understand what you want to ask and if you want to name of  contact then below code will help you.modify given code as your requirement. 

for(Account a:[select Name,(select id,name from contacts),(select id from opportunities)  from Account limit 5])
  {
   for(Contact con : a.contacts)
  {
    Contact conname=con.name;
  }

}
Dilip KrDilip Kr
hi ravikant,

 lots of appreciation  for your  support.
 when i click on count of contact,it would display name of all contact of that account.
eg-   account name                      total contact
        United Oil & Gas, UK1           1

    on click on 1 ,it will display contact name associated with United Oil & Gas, UK1

my code is not displaying associated contact of account.please let me know what i missing in code.


thanks
Ravikant kediaRavikant kedia
Hi dilip kr,
                 I solved your problem use belw code and you can modify below code as your requirement. I change something in your code .

//page code
<apex:page controller="sampleCode">

<apex:form >

<apex:outputPanel id="ContactDetail">

<apex:repeat value="{!contactsInformation}" var="contact">
<p>{! contact.FirstName & ' ' & contact.LastName}</p>
</apex:repeat>
</apex:outputPanel>

<apex:pageBlock >
     <apex:pageBlockTable value="{!wrapperList}" var="b" >
                         <apex:column value="{!b.Name}" headerValue="Account Name"/>
                   
                         <apex:column headerValue="Total Contacts">  
                             <apex:commandlink action="{!accountClicked}" rerender="ContactDetail">
                             <apex:outputText value="{! b.con}"/>
                               <apex:param name="id" value="{!b.recordid}" assignTo="{!selectedcountofcontact}"/>
                         </apex:commandLink>                             
                         </apex:column>
                         
                         <apex:column headerValue="Total Opportunities">
                             <apex:commandLink value="{!b.Opp}"/>
                         </apex:column>                     

    </apex:pageBlockTable>
</apex:pageBlock>
</apex:form>

</apex:page>



//Apex code


public with sharing class sampleCode {

public List<wrapper> wrapperList{get; set;}
public List<Contact> contactsInformation { get; set; }
public Id selectedCountOfContact { get; set; }


public sampleCode(){
         wrapperList=new List<Wrapper>();
         for(Account a:[select id,Name,(select id,name from contacts),(select id from opportunities)  from Account limit 5])
         {
           Wrapper w1=new Wrapper();
           w1.Name=a.Name;
           w1.Con=a.contacts.size();
              
           w1.Opp=a.opportunities.size();
           w1.recordid =a.id;
           wrapperList.add(w1);
         }       
}

class Wrapper{
    public String Name{get; set;}
    public Integer Con{get; set;}
    public Integer Opp{get; set;}   
    public Id recordid{get; set;}
 }
 
public List<Account> getMyAccounts() {
return [SELECT Id, Name,Industry, AccountNumber FROM Account ORDER BY
LastModifiedDate DESC LIMIT 10];
}

public void accountClicked() {
contactsInformation = [SELECT FirstName, LastName FROM Contact
WHERE AccountID = :selectedCountOfContact];
  }
}
I checked it and now it's working as your requirement and select best answer if it solve your problem .
This was selected as the best answer
Dilip KrDilip Kr
Hi ravikant,

      thanks for your support.


vara prasad 4vara prasad 4
Hi when ever i click opportunitys not perform any action.