+ Start a Discussion
Sam Miller 8Sam Miller 8 

Represent multiple records in a single line in a visualforce page


I have been able to get a visualforce page working to display the results of a query with one row for each record but would like to see if it is possible to format the data differently so that I can group by a field and then display all of the values in another field in the rest of the row. An example would be:

FIELD 1      FIELD 2         FIELD 3
A                 1                     Red
A                 2                     Yellow
B                 2                      Yellow
B                 1                      Red
B                 3                      Green

To be displayed in separate cells as:

A      1(in red)        2(in yellow)
B      2(in yellow)    1(in red)        3(in green) 

Thanks in advance for any assistance.
Hi Sam,

You might could use an AggregateResult and loop through the rusults and return how you like......
AggregateResult[] AggResults = [select  field1__c, sum(field2__c), field3__c from test__c group by field1__c, field3__c];

for (AggregateResult ar : AggResults)  {
    System.debug('Type: ' + ar.get('field1__c'));
    System.debug('Sum: ' + ar.get('expr0'));
    System.debug('Color: ' + ar.get('field3__c'));    


Sam Miller 8Sam Miller 8
Hi CJWilder,

Thanks for the response. However, I do not want to sum the data in Field 2 since it would be possible for me to get two records with the same values in Field 1 and Field 3 but different values in Field 2 that I would like to see separately when displayed so not sure that the aggregate function produces the desired output.

Hey Sam,

So if you had a table with the data below the soql would give you the soql result. Are you wanting the soql result or something like the other result. The soql reuslt would just be the first step the next would be to loop through in and format to pass back to the VF page.

field1__c field2__c field3__c
A             3              Yellow
A             2              Yellow

A             1               Red
B             3              Green
B             2              Yellow
B             1              Red

SOQL Result:
field1__c Sum  field3__c
A             1        Red
A             5        Yellow
B             1        Red
B             3        Green
B             2        Yellow

Other Result

A   1(in Red) 2(in Yelow) 3(in Yellow)
Sam Miller 8Sam Miller 8
Hi CJWilder,

Thanks for the response. My main challenge here is how to render these results in the VF page since I can get rthe data displaying one record at a time but want to have multiple records being displayed on a signle line, all grouped by Field 1.