You need to sign in to do that
Don't have an account?
Travis Malle 9
Parsing grouped results to display in visualforce
Hello Community,
I’m attempting to use one SOQL query to get many data point that can be displayed on a Visualforce page. My query returns the expected results but I am not sure of how to parse through the query results and assign them to variables. My thought was that I could simply loop through the results and, based on the conditions, assign them to variables with getters and setters. This does not generate an error, but my page will anything when calling {!MembershipsYTD}. if someone could help point me in the right direction it would be greatly appreciated.
public class MDEventResults
{
// Properties
public integer ThisMonth = date.today().month();
public integer MembershipsYTD {get; set;}
public integer MembershipsMTD {get; set;}
// Query Event Data
List <AggregateResult> EventAgg = [SELECT Sum(Total_Memberships__c) MembershipSum, count(ID) CNT, Calendar_month(Event_Date__c) Month, Facilitated_by_MD__c FacilitatedBy, Booked_By__c BookedBy,
GROUPING (Facilitated_by_MD__C) FacilGroup, GROUPING (Booked_By__c) BookedGroup, GROUPING (Event_Date__c) monthGroup
FROM Campaign
// WHERE Booked_by__r.id = :UserInfo.getUserid()//
WHERE End_Date_Time__c = This_Year
// AND Status = 'Completed' //
GROUP BY ROLLUP (Facilitated_by_MD__c , Booked_By__c, Event_Date__c)];
// method to parse results
public void MembershipsYTD() {
for (AggregateResult ar : EventAgg) {
if (integer.valueOf(ar.get('monthGroup' )) == ThisMonth &&
integer.valueOf(ar.get('BookedGroup')) == 1 &&
integer.valueOf(ar.get('FacilGroup' )) == 1)
{
MembershipsYTD = integer.valueOf(ar.get('MembershipSum'));
system.debug('MembershipsYTD' + MembershipsYTD);
}
}
}
}
I’m attempting to use one SOQL query to get many data point that can be displayed on a Visualforce page. My query returns the expected results but I am not sure of how to parse through the query results and assign them to variables. My thought was that I could simply loop through the results and, based on the conditions, assign them to variables with getters and setters. This does not generate an error, but my page will anything when calling {!MembershipsYTD}. if someone could help point me in the right direction it would be greatly appreciated.
public class MDEventResults
{
// Properties
public integer ThisMonth = date.today().month();
public integer MembershipsYTD {get; set;}
public integer MembershipsMTD {get; set;}
// Query Event Data
List <AggregateResult> EventAgg = [SELECT Sum(Total_Memberships__c) MembershipSum, count(ID) CNT, Calendar_month(Event_Date__c) Month, Facilitated_by_MD__c FacilitatedBy, Booked_By__c BookedBy,
GROUPING (Facilitated_by_MD__C) FacilGroup, GROUPING (Booked_By__c) BookedGroup, GROUPING (Event_Date__c) monthGroup
FROM Campaign
// WHERE Booked_by__r.id = :UserInfo.getUserid()//
WHERE End_Date_Time__c = This_Year
// AND Status = 'Completed' //
GROUP BY ROLLUP (Facilitated_by_MD__c , Booked_By__c, Event_Date__c)];
// method to parse results
public void MembershipsYTD() {
for (AggregateResult ar : EventAgg) {
if (integer.valueOf(ar.get('monthGroup' )) == ThisMonth &&
integer.valueOf(ar.get('BookedGroup')) == 1 &&
integer.valueOf(ar.get('FacilGroup' )) == 1)
{
MembershipsYTD = integer.valueOf(ar.get('MembershipSum'));
system.debug('MembershipsYTD' + MembershipsYTD);
}
}
}
}
VF Page
All Answers
One way and the most common way for such kind of scenario is
Use a wrapper class, create the wrapper with both MTD and YTD propertise and populate the list of that wrapper and pass it to your page.
Other way will be to use Analytics API to avoid lot of code. Check out my blog where you can create a report and use it in a VF page
https://vivekdeepak.wordpress.com/2015/06/28/salesforce-analytics-api-with-angularjs-part-1/
That’s kinda what I was thinking, I tried to create a wrapper class and pass the results but couldn’t figure out how to make it work (I’m still a novice to ) would you be able to post a dumbed down example of passing this information to the wrapper class?
Thank you again sir, your help is greatly appreciated
VF Page
I’m at a bit of a loss on this one… Can you help me understand the ‘why’ behind using a wrapper class. My understanding was that a wrapper class is commonly used when trying to generate lists of various objects. In my scenario, I’m simply trying to pull more than one piece of data from an SOQL query. I am able to accomplish what I want by using a controller that has an SOQL query and a get method for every datapoint I need; although this works, it seems incredibly inefficient. My goal was to use a query leveraging the grouping feature to get more than one data point at a time… feel like I’m missing the mark here. Can you help me understand
Here is the code I’m currently using, it works, but I think it can be done in a more efficient way (less queries)