0

VF Page:

<apex:page standardController="Contact" extensions="AggregateController">
    <apex:form >
    <script>
function displayErr(){
sendredirect();
}
</script>
  <apex:pageBlock >
 <apex:pageBlockTable value="{!resellerlist}" var="o">
<apex:column >
<apex:facet name="header">Account Name</apex:facet>
{!o['name']} </apex:column>
<apex:column >
<apex:facet name="header">No of contacts</apex:facet>
<apex:outputLink onclick="dislayErr()">{!o['ids']}</apex:outputLink> </apex:column>
  </apex:pageBlockTable>

  <apex:outputLink ></apex:outputLink>

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

Controller:

public with sharing class AggregateController {
public List<AggregateResult> groupedResults{get; set;}
//public string searchstring{get;set;}
public Integer total_used{get;set;}
//public list<Contact> listcon{get; set;}

    public AggregateController(ApexPages.StandardController controller) {
groupedResults=[Select Account.Name name, Count(Id) ids From Contact Group By  Account.Name limit 100];
system.debug('groupedResults'+groupedResults[0]);
    }
    public list<AggregateResult> resellerlist {
 get { return groupedResults;}
 }
 public PageReference sendredirect(){

 return null;
 }
    /*public PageReference search(){
    groupedResults=[SELECT COUNT(Id) FROM Contact, Contact.Account ];
    listcon=[select LastName from Contact where Account.Name=:searchstring];
    system.debug('listcon'+listcon);
    system.debug('groupedResults'+groupedResults);
    total_used = Integer.valueOf(groupedResults[0].get('expr0') + '');
system.debug('total_used '+total_used );
    return null;
    }*/


}

hi i made an attempt to show each account with number of contacts, if i click on count it has to redirect to account detail page, here i had used list

///please suggest me what to do

5
  • i want to query Account id how to do this,,,help
    – Satya
    Apr 28, 2016 at 12:31
  • Each contact has a field on it named contactId. Use that to query the account.
    – crmprogdev
    Apr 28, 2016 at 13:16
  • hi,,it is not working
    – Satya
    Apr 29, 2016 at 4:32
  • //Select Account.Name name,AccountId accid,Count(Id) ids From Contact Group By Account.Name,AccountId limit 100 using this i got it,,but i have to use accid in pagereference ,thing is it is not allowing,showing error
    – Satya
    Apr 29, 2016 at 5:04
  • how to pass {!o['accid']} this from vf page to controller
    – Satya
    Apr 29, 2016 at 6:02

2 Answers 2

0

You don't need to handle a redirect in the controller. You can use the URLFOR() function in your VF page.

<apex:outputLink value="{!URLFOR($Action.Account.View, o['accId'])">{!o['ids']}</apex:outputLink>

You pass in the $Action for viewing the Account, and the Id of the Account to view. You can modify your Aggregate SOQL query like so to accomplish this.

Select Account.Name name, AccountId accId, Count(Id) ids From Contact Group By  Account.Name, AccountId limit 100
2
  • Hi Doug,i tired and i got this error,nvalid parameter for function URLFOR Error is in expression '{!URLFOR($Action.Account.View, o['accId'])}' in component <apex:outputLink> in page sahi:usingaggrega
    – Satya
    Apr 30, 2016 at 7:21
  • i made simple modification like below and it is working thanks,<apex:outputLink value="/{!o['accid']}" >{!o['ids']} </apex:outputLink>
    – Satya
    Apr 30, 2016 at 7:22
0

I don't think you understood my original comment. From what I can see, it appears there may be some code missing. I see where you've commented out some other code and don't understand it's relevance to your question. For that reason, I may not be able to give you a complete answer since variables on your page don't appear to be referenced in your controller extension, making it unclear where they came from.

It's unclear to me show us how you get the contacts to begin with, as in whether you use the search string method that's commented out or some other method from a different controller. Regardless, your query needs to return the AccountId field from Contact in addition to the Account.Name field that you seem to be querying. That's where and how you get the Account Id.

In your comments, you wrote that you had problems when you tried the following query:

Select Account.Name name, AccountId accid, Count(Id) ids From Contact Group By Account.Name,AccountId limit 100

I'd expect that you would have problems with the above. When you try to do the GroupBy on Account.Name you're using a dot notation which the aggregate results can't handle. Sadly, if you alias it, the query doesn't handle that very well either.

It would seem that you'd want to group by AccountId or Account.Name then get the Count of contact records/Ids within each AccountId or Account Name group. One way of doing that would be to query on Account instead of Contact. If you're populating the list to begin with from a list of contacts, that would be easy to do, but would obviously require two queries. If you populate it from Account, you could also do it with a subquery and sort the results to get the counts, but I don't know how well a subquery would work if performed from within an aggregate results query; especially one that contained a GroupBy clause. I've never seen that done before.

To do what you need just starting from Contacts, I think you'd need to do it along the lines of something like the below which works for me in my DE org:

 list<aggregateresult>ar = [SELECT count(Id) cnt, Account.Name FROM Contact Group By Account.Name LIMIT 100];
system.debug('size = ' + ar.size());
for(aggregateresult a:ar){
//system.debug('Account Name ='+ a.Name + ' Num Contacts = ' + a.cnt);
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .