1

I have a custom object which requires booleon values which are attached to contacts like the following:

Contact     Option1     Option2     Option3
============================================
Bob         true        true        false
Dave        false       true        false
Sally       false       false       false

I need to display these results in a table and then also display the total for each option at the bottom.

How can I achieve this? here is what I have so far...

APEX:

public with sharing class bbeventlist {

    public list<RSVPData__c> lstItem {get;set;}

    public list<RSVPData__c> getRSVPData(){
        lstItem = new list<RSVPData__c>();

        String query = 'SELECT Id, Name, Option1, Option2, Option3 FROM RSVPData__c ORDER BY CreatedDate DESC';

        lstItem = Database.query(query);
        return lstItem;
    }

}

Page:

<apex:page id="optionsList" showHeader="true" title="Options Lits" controller="bbeventlist">
<apex:define name="body">  

<h1>Options List</h1>

<apex:form >
    <apex:pageBlock title="Attendees" mode="detail">
        <apex:pageBlockButtons >
        </apex:pageBlockButtons>

        <apex:pageBlockTable value="{!RSVPData}" var="rsvp">
            <apex:column value="{!rsvp.ContactID__c}"/>
            <apex:column value="{!rsvp.Option1__c}"/>
            <apex:column value="{!rsvp.Option2__c}"/>
            <apex:column value="{!rsvp.Option3__c}"/>
        </apex:pageBlockTable>

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


</apex:define>
</apex:page>

Desired output:

Contact     Option1     Option2     Option3
============================================
Bob         true        true        false
Dave        false       true        false
Sally       false       false       false
--------------------------------------------
Total       1           2           0
5
  • If you are going to downvote, please explain why so I can improve the question Commented Jan 21, 2015 at 11:56
  • The question seems fine to me. I've added an answer for calculating the totals, but does generating the rest of the table already work? Or is there an issue there as well? Commented Jan 21, 2015 at 13:05
  • 1
    You can also check my old solution for calculating totals without apex (visualforce only): To achieve Grand total for the retrieved column in a datatable generated using Soql Commented Jan 21, 2015 at 13:15
  • @mastor The problem with your solution is that it SUMs the value inside the column. I want to COUNT each instance of true, the fields are Booleans and not Integers. Commented Jan 21, 2015 at 13:22
  • It is not a big problem to increment a variable each time if some value is true... Up to you. Commented Jan 21, 2015 at 13:38

2 Answers 2

1

This may not be the ideal answer. :) But you can try.
Declare three integer variables:

public Integer count0{get;set;}
public Integer count2{get;set;}
public Integer count3{get;set;}

Then, use the list you have built to calculate the count of true's. Like this:

count0 = 0;
count2 = 0;
count3 = 0;
for(RSVPData__c in1 :lstItem )
        {

            if(in1.Option1__c)
            {
                count0++;
            }
            if(in1.Option2__c)
            {
                count2++;
            }
            if(in1.Option3__c)
            {
                count3++;
            }
}

Then, in the VF page, add the facet footer like this:

<apex:column value="{!rsvp.Option1__c}">
    <apex:facet name="footer">
      {!count0}
    </apex:facet>
</apex:column>
2

You will have to write a get-method (Integer) for every option. I have written an example for option one below. I have taken out the for-loop and put it in another method, so all three options can be calculated in one for-loop.

public Integer optionOne {get;set;}
public Integer optionOneVal;

public Integer getOptionOne(){
   if(this.optionOneVal == null)
      this.calcOptions();
   return this.optionOneVal;
}

private static void calcOptions(){
   this.optionOneVal = 0;
   for(RSVPData__c rsvp : lstItem){
      if(rsvp.Option1)
         this.optionOneVal++;
   }
}

Keep in mind that I wrote this in a textbox, please let me know if it doesn't work.

3
  • The rest of the table does work as expected. I have tried your code but I'm getting a 'variable does not exist' error: pastebin.com/zyiFhKzm. Commented Jan 21, 2015 at 13:31
  • Does it tell you which variable or which line? Commented Jan 21, 2015 at 14:11
  • It was refering to line 25, I have used Shailesh's solution which I believe is quote similar to yours. Commented Jan 21, 2015 at 14:32

You must log in to answer this question.

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