+ Start a Discussion
EzmoEzmo 

Creating report using SOQL

Hi, I'm trying to create a report in a visualforce page for a custom object called Skills__c.

What I'm wanting to get is a table to show how many times a picklist option has been selected in a picklist called skill__c, to help understand what I mean here is an example:

 

Skill Total

Attention to Detail 5

Time Management 2

Reporting 1

 

Something like that.

 

So my question is, does anyone know any places I can find how to do this, I've found a few examples but not for custom objects and I'm struggling to change them. 

 

Thanks

 

Ezmo

Best Answer chosen by Admin (Salesforce Developers) 
sravusravu

Try the following code :

 

 

public class reportClass {

      public list<AggregateResult> lstAr = new list<AggregateResult>();

      public reportClass()
 { lstAr = [select Skill__c Name, count(Id) Total from Skills__c group by Skill__c]; }
public list<reportClassResult> getResults() { list<reportClassResult> lstResult = new list<reportclassResult>(); for(AggregateResult ar : lstAr) { reportClassResult objclass = new reportClassResult(ar); lstResult.add(objclass); } return lstResult; } class reportClassResult { public Integer Total {get;set;} public String Name {get;set;} public reportClassResult(AggregateResult ar) { Total = (Integer) ar.get('Total'); Name = (String)ar.get('Name'); } } }

 

 

All Answers

MiddhaMiddha

You can use Aggregate queries and group by on Skill__c.

sravusravu

Try the following code :

 

 

public class reportClass {

      public list<AggregateResult> lstAr = new list<AggregateResult>();

      public reportClass()
 { lstAr = [select Skill__c Name, count(Id) Total from Skills__c group by Skill__c]; }
public list<reportClassResult> getResults() { list<reportClassResult> lstResult = new list<reportclassResult>(); for(AggregateResult ar : lstAr) { reportClassResult objclass = new reportClassResult(ar); lstResult.add(objclass); } return lstResult; } class reportClassResult { public Integer Total {get;set;} public String Name {get;set;} public reportClassResult(AggregateResult ar) { Total = (Integer) ar.get('Total'); Name = (String)ar.get('Name'); } } }

 

 

This was selected as the best answer
EzmoEzmo

That works perfectly thank you VERY much!! :smileyvery-happy:

 

Ezmo