You need to sign in to do that
Don't have an account?
Andy Morton 14
Show number of cases by contact
Hello
I have a visualforce page (CustomerServiceReport) that uses the standard Account controller and an extension (CustoemrServiceReportExtension) to pull through some basic information about cases relating to that Account (i.e. number of cases raised in last 30 days, number closed, etc).
I want to expand on this to show the contacts and number of cases they have raised in the last 30 days (ideally restricted to the top 10 with ordered by highest to lowest). For the life of me I don't seem to be able to get it working. All I will need is the Contact's Name the case is raised under and the number of cases they have raised.
I'm fairly new to Visualforce and Apex so still trying to get to grips with it. This is a bit of a side-project (which may make my employer's life a bit easier) that I figure may help me improve my skills.
Anyone able to help me out? I'm fine with it been a secondary extension if it makes things easier/tidier.
I have a visualforce page (CustomerServiceReport) that uses the standard Account controller and an extension (CustoemrServiceReportExtension) to pull through some basic information about cases relating to that Account (i.e. number of cases raised in last 30 days, number closed, etc).
I want to expand on this to show the contacts and number of cases they have raised in the last 30 days (ideally restricted to the top 10 with ordered by highest to lowest). For the life of me I don't seem to be able to get it working. All I will need is the Contact's Name the case is raised under and the number of cases they have raised.
I'm fairly new to Visualforce and Apex so still trying to get to grips with it. This is a bit of a side-project (which may make my employer's life a bit easier) that I figure may help me improve my skills.
Anyone able to help me out? I'm fine with it been a secondary extension if it makes things easier/tidier.
here a sample code how you can get different fields
List<AggregateResult> ArResult = [select count(id),contact.name,accountid from case where contactid!= null and accountid=:acct.id AND CreatedDate <= LAST_N_DAYS:30 group by contact.name , accountid order by count(id) desc];
for (AggregateResult ar : ArResult ) {
system.debug(ar); //complete row
system.debug(ar.get('expr0')); //count
system.debug(ar.get('Name')); //contact name
}
All Answers
i assume you can use the same approach you are using for showing data by account.
following query will provide data for cases by contact for given account. for vf page implemmetation use same approach you used for account.
select count(id),contact.name,accountid from case where contactid!= null and accountid = 'Your_account_id_from_vf_page' group by contact.name , accountid order by count(id) desc
The Aggregate Query solution provided by gaurav will work great for you. If you have trouble implementing it, just post your vf / apex code and he may guide you about his solution!
I've added the query onto a new extension but getting a failure when saving: Illegal Assignment from List to List. Have been looking through some forums but can't seem to see what's causing it (probably something obvious I'm missing).
here a sample code how you can get different fields
List<AggregateResult> ArResult = [select count(id),contact.name,accountid from case where contactid!= null and accountid=:acct.id AND CreatedDate <= LAST_N_DAYS:30 group by contact.name , accountid order by count(id) desc];
for (AggregateResult ar : ArResult ) {
system.debug(ar); //complete row
system.debug(ar.get('expr0')); //count
system.debug(ar.get('Name')); //contact name
}
That's great, working as intended - many thanks for your help.