1

I have created 1 object application which is related to contact. i.e Lookup relationship b/w contact and application. Created Contact field as lookup on application object In vf page i want to show all contacts and count of application per contact besides contact name. I am trying out this code

enter image description here

Class code:

public with sharing class theController 
{
    Set<ID> ContactIds = new Set<ID>();
    Set<ID> UserIds = new Set<ID>();

    public Contact contact{get;set;}

    public List<Contact> lstContact{get;set;}
    public List<User> lstUser{get;set;}

    public theController()
    {
            lstUser = [Select Id,ContactId from user where IsActive=true and IsPortalEnabled = true];

            for(User user: lstUser)
            {
                contactIds.add(user.ContactId);
                UserIds.add(user.Id);
            }

            lstContact=[Select Name,AccountId from Contact where Id IN: contactIds];

            if(lstContact != null && lstContact.size()>0)
            {
                this.contact = lstContact[0];
            }

            Integer countapplication =[Select count() from Application__c where Contact__c IN : contactIds];
            system.debug('## countapplication' + countapplication);
    }
}

No getting the count of application.

0
public  Map<string,integer> ansalpha {
get {
   AggregateResult[] groupedResults=[SELECT COUNT(),Contact__c FROM  Application__c where Contact__c IN : contactIds GROUPBY Contact__c];

  Map<string,integer> mapCntIdByapplicationCount=new Map<string,integer>();

     for(AggregateResult a:groupedResults){
         mapCntIdByapplicationCount.put(String.valueof(a.get('Contact__c')),integer.valueof(string.valueof(a.get('expr0'))));
  }

   return mapCntIdByapplicationCount; 
  }
 set;
} 

You can use aggregate SOQL to accomplish this as shown above .Form a map and then you can get from the map for your visualforce

<apex:repeat value="{!lstContact}" var="c">
  <apex:outputField value="{!c.Name}"/>
  <apex:outputText value="Value="{!ansalpha[c.Id]}" />"/>

0

Assuming the child relationship is called Applications__c you should do this as part of your main query on the Contact object:

lstContact = [Select Name, AccountId, (select Id from Applications__r) from Contact where Id IN: contactIds];

And then in your page you can simply use the size of the returned list for each:

<apex:repeat value="{!lstContact}" var="c">
  <apex:outputField value="{!c.Name}"/>
  <apex:outputText value="{!Floor(c.Applications__r.Size)}"/>
</apex:repeat>

The reason your code isn't working is that you're just getting the total count of applications for all of the contacts being displayed.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.