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
Nagaraju MogiliNagaraju Mogili 

how can we show the number of contacts of an account using apex class

I want to show the number of contacts of an Account in the vf page using Apex code and Vf page..
 
Best Answer chosen by Nagaraju Mogili
Nagaraju MogiliNagaraju Mogili
Hi Sharad,

Thanks for your code.

I am getting the error at 13 and 14 lines in the controller,could you please explain me why..?


Error: NewController_cntrlr Compile Error: Loop variable must be a generic SObject or List or a concrete SObject or List of: Contact at line 12 column 12,,

public class NewController_cntrlr {


    public Map<Id ,List< Contact>> mapToshowContactCount{get;set;}
    
    public NewController_cntrlr () {
        
        mapToshowContactCount = new Map<Id , List< Contact>>();
        List<Account> accList =  [Select Id , (Select  Name FROM Contacts) FROM Account ];
        for(Account acc : accLIst) {
            
           for(Contact con : acc.Contacts) {
                if(!mapToshowContactCount.containsKey(acc.Id)) {
                    mapToshowContactCount.put(acc.Id, new List<Contact>{con});
                }
                else {
                    mapToshowContactCount.get(acc.Id).add(con);
                }
            }
            
        }
        System.debug(mapToshowContactCount);
        
    }
}


Regards,
Nagaraju Mogili

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Nagaraju,

May I suggest you please check with below link from the stack exchange community which will point you further in the right direction. Let us know if this helps.

Kindly mark this as solved if it's resolved.

Thanks,
Nagendra
Deepak Pandey 13Deepak Pandey 13
Hello Nagaraju Mogili,
     
You just query on account(Parent object) with inner query(child object) and take thier size and show in account field.
Like ->
setid = Current Record id
Account objacc = [select id, Name, (Select id, LastName from Account.Contacts) from Account where id in: setid ]; 

Account Acc = new Account();
Acc .fieldcoun__c = objacc.Contacts.size();
Acc .id= objacc.id;
update Acc ;
 
Varun SinghVarun Singh
Hi

it will show  no. of contact for particuler account.

way to access your vf page

https://varun007-dev-ed--c.ap5.visual.force.com/apex/getrelatedcon?id=0017F00000ExzeM
here getrelatedcon is my vf page
0017F00000ExzeM id of a accout which have  5 contacts

output is --> The total number of contacts on Account is: 5


Controller
public class contactsOnAccountsExtension{
private final Account acct;
public Integer sum {get; set;}

        public contactsOnAccountsExtension(ApexPages.StandardController stdController) {
        this.acct = (Account)stdController.getRecord();
        sum = [SELECT count() FROM Contact WHERE AccountId =:acct.Id];            
    }
}
Page
<apex:page standardController="account" extensions="contactsOnAccountsExtension">
<apex:outputLabel value="The total number of contacts on Account is: "/>
<apex:outputText value="{!sum}"/>
</apex:page>

I hope this is  helpful for  you.
Sharad SoniSharad Soni

Hi Nagaraju,

I have something similar answer to your problem. I have created an inline VF page where contact is the parent of the custom object Transaction Entries and this VF page is showing the count of the child records under the contact in the inline VF page

VF Page

<apex:page standardController="Contact" extensions="countTotalChilds" >
    <apex:form>
        <apex:pageBlock>
            <apex:outputText value="{!countRecords}" ></apex:outputText> 
        </apex:pageBlock>
    </apex:form>
</apex:page>

Controller

public class countTotalChilds {
    
    //ApexPages.standardController instance
    ApexPages.StandardController stcon;
    
    //Variable to get the Current Page Id
    Id accId{get;set;}
    
    //Variable to hold the count of the number of the records
    public Integer countRecords{get;set;}
    
    //Class constuctor
    public  countTotalChilds(ApexPages.StandardController st) {
        
        //Setting the values
        stcon = st;
        accId = ApexPages.currentPage().getparameters().get('Id');
        List<AggregateResult> trans = [Select count(Id) custId FROM Transaction_Entries__c WHERE Contact_Name__c =: accId];
        countRecords = (Integer)trans[0].get('custId');
        
    }
}

Please Mark it best answer if you find this helpful.
Thanks
Nagaraju MogiliNagaraju Mogili
Hi Sharad,

Thanks for your Reply.

Do we have any chances to do this task by using Map, I want to show all of the Accounts and related contacts in VF page.

Regards,
Nagaraju
Sharad SoniSharad Soni
Hi Nagaraju, I think I did not get your question properly.Can you please explain your question once again. Thanks.
Nagaraju MogiliNagaraju Mogili
I want to show the contacts of every account in the VF page using the Apex class and Map.
Sharad SoniSharad Soni
If I am not wrong I think you want to show the contact records under the account on vf page table. Right?
Nagaraju MogiliNagaraju Mogili
I mean that, How many contacts are there for each account, have to display by using Map in the Apex Class
Sharad SoniSharad Soni
Try this One too , not so many changes just a change in the look of the vf page VF page
Sharad SoniSharad Soni
Controller

public class NewController {

    public Map<Id ,List< Contact>> mapToshowContactCount{get;set;}
    
    public NewController() {
        
        mapToshowContactCount = new Map<Id , List< Contact>>();
        List<Account> accList =  [Select Id , (Select  Name FROM Contacts) FROM Account ];
        for(Account acc : accLIst) {
            
            for(Contact con : acc.Contacts) {
                if(!mapToshowContactCount.containsKey(acc.Id)) {
                    mapToshowContactCount.put(acc.Id, new List<Contact>{con});
                }
                else {
                    mapToshowContactCount.get(acc.Id).add(con);
                }
            }
            
        }
        System.debug(mapToshowContactCount);
    }
}
 
VF Page

<apex:page Controller="NewController"  id="thePage"> 
    <apex:form id="theForm"> 
        <apex:pageBlock id="thePageBlock"> 
           <apex:pageBlockTable value="{!mapToshowContactCount}" var="a">
               <apex:column headerValue="Account Id"  >
              
               <apex:outputText value="{!a}" ></apex:outputText>
               </apex:column>
               <apex:column >
             <apex:PageBlockTable value="{!mapToshowContactCount[a]}" var="v">
                 <apex:column headerValue="Contact">
               <apex:outputText value="{!v.Name}" ></apex:outputText>
                 </apex:column>
                 </apex:PageBlockTable>
               </apex:column>
            </apex:pageBlockTable>
        </apex:pageBlock> 
    </apex:form>
</apex:page>
Nagaraju MogiliNagaraju Mogili
Hi Sharad,

Thanks for your code.

I am getting the error at 13 and 14 lines in the controller,could you please explain me why..?


Error: NewController_cntrlr Compile Error: Loop variable must be a generic SObject or List or a concrete SObject or List of: Contact at line 12 column 12,,

public class NewController_cntrlr {


    public Map<Id ,List< Contact>> mapToshowContactCount{get;set;}
    
    public NewController_cntrlr () {
        
        mapToshowContactCount = new Map<Id , List< Contact>>();
        List<Account> accList =  [Select Id , (Select  Name FROM Contacts) FROM Account ];
        for(Account acc : accLIst) {
            
           for(Contact con : acc.Contacts) {
                if(!mapToshowContactCount.containsKey(acc.Id)) {
                    mapToshowContactCount.put(acc.Id, new List<Contact>{con});
                }
                else {
                    mapToshowContactCount.get(acc.Id).add(con);
                }
            }
            
        }
        System.debug(mapToshowContactCount);
        
    }
}


Regards,
Nagaraju Mogili
This was selected as the best answer
Sharad SoniSharad Soni
Hi Nagaraju,

I don't know why you are getting this error, I've created the same code in my org and it is running smoothly. Here is the snap of the VF page output. Also is the VF page is same as I sent?? 
User-added image
Nagaraju MogiliNagaraju Mogili
Hi Sharad,

Still I am getting the same Error, can you text me your Skype Id.

I have tried in another Org as well.

Regards,
Nagaraju Mogili
Sharad SoniSharad Soni
live:cb6e25666f56a153
Nagaraju MogiliNagaraju Mogili
I am getting this error....

User-added imageUser-added image
Nagaraju MogiliNagaraju Mogili
User-added image
Sharad SoniSharad Soni
Hi Nagaraju

Can you please ping me on skype, I have already sent you my skype Id.
Sharad SoniSharad Soni
Hi Nagaraju, 

Here is your code
 
<apex:page Controller="NewController_cntrlr"  id="thePage"> 
    <apex:form id="theForm"> 
        
        <apex:pageBlock id="thePageBlock"> 
            
            <apex:variable var="count" value="{!0}" />
            
            <apex:pageBlockTable value="{!mapToshowContactCount}" var="a">
                
                <apex:column headerValue="Account Id"  >
                    <apex:variable var="count" value="{!0}" />
                    <apex:outputText value="{!a}" ></apex:outputText>
                </apex:column>
                
                <apex:column headerValue="Contacts">
                    
                    <apex:PageBlockTable value="{!mapToshowContactCount[a]}" var="v">
                        
                        <apex:variable var="count" value="{!0}" />
                        
                        <apex:column headerValue="Contact">
                            <apex:outputText value="{!v.Name}" ></apex:outputText>
                            <apex:variable var="count" value="{!count + 1}" />
                        </apex:column>
                        
                    </apex:PageBlockTable>
                    
                </apex:column>
                
                <apex:column headerValue="total" >
                    <apex:outputText value = "{!count}"></apex:outputText>
                </apex:column>
                
            </apex:pageBlockTable>
        </apex:pageBlock> 
    </apex:form>
</apex:page>


Mark, it as best answer if you find it useful so that other developers can follow.
Thanks.

Regards Sharad.