0

Below is my pathetic attempt at VF and Apex. The two are barely talking to one another. Let's get them to be friends. What do I need to change to get the variables in Mr. Controller to display in Mrs VF page. (I can just see the moderators rip me for this one. Stupid and annoying people do not do well on this list)

I need memTypes, orgs and eventually contact__r.Name to show in a HTML table on Mrs VF but first lets have the two pages talk without needing a lawyer. What does on the apex:repeat line and what goes on the apex:outputtext line?

Mrs. VF Page:

<apex:repeat value="{!orgs}" var="string" id="theRepeat">

    <apex:outputText value="{!orgs}" id="theValue"/><br/>

</apex:repeat>

Mr Controller page


public class vfCtrlr_BOG_Committee_Report {

    public String[] getOrgs() 
    {
        List bogQueryList = [Select BOG_Membership_Type_New__c, ORG__c, contact__r.Name 
        FROM BOG_Membership__c 
        WHERE BOG_Membership_Status__c = 'Active' 
        AND BOG_Membership_Type_New__c  '' 
        ORDER BY BOG_Membership_Type_New__c , ORG__c, contact__r.Name ];

        String[] memTypes = New List ();
        String[] orgs = New List ();    

        for(BOG_Membership__c record :bogQueryList)
        {
            memTypes.add((String) record.get('BOG_Membership_Type_New__c'));
            orgs.add((String) record.get('ORG__c'));
        }

        return orgs;

    }
}
  • Make it <apex:outputText value="{!String}" id="theValue"/> – Prajith Mar 17 '16 at 8:57
  • 3
    I do like the title ;) , but isn't it better to keep the title technical, like "access list element on apex repeat" as it will help other users searching for similar issues. Thanks – Prajith Mar 17 '16 at 9:00
  • Ok. I accept and changed the title. Can you explain what goes on the repeat line and what goes on the output text line? Also, I really want this as a Set not Not a List to avoid duplicates. How to change the lines in the middle (there are no lines numbers) to reflect it and return it properly so the VF page will display all values? – Apex N-u-b Mar 17 '16 at 9:21
  • Sure, apex repeat is like a "for each" loop on the page. So in the <apex:repeat> tag -> 'value' takes in a collection element from the controller(you have given a list of strings in this place). The 'var' attribute, asks you to give a name, using which you will refer to each instance of the collection inside the apex:repeat tag. This is similar to the instance variable you define in a for each loop(you gave the name "string">. So basically inside the apex:repeat tag, variable "string" means -> the current value of the string element of your list(which you passed as 'value' to apex:repeat) – Prajith Mar 17 '16 at 9:31
  • To change it to a set, you only need to create the appropriate set in the controller and then in the VF page pass the set as the "value" of apex:repeat -> rest will work fine. – Prajith Mar 17 '16 at 9:34
2

Your Mrs. Visualforce page should be look like:

<apex:repeat value="{!orgs}" var="BOGMembership" id="theRepeatId">

    <apex:outputText value="{!BOGMembership}" id="theValue"/><br/>

</apex:repeat>
  • Attribute value accept the List type variable.
  • Attribute var is a reference variable to use during <apex:repeat> iteration. So, it can be any relevant name which make sense.

String can be directly used by expression as:

 <apex:repeat value="{!orgs}" var="BOGMembership" id="theRepeatId">

    <apex:outputText value="{!BOGMembership}" id="theValue"/><br/>

 </apex:repeat>

Below is refactored code:-

Mr. Controller:

public class vfCtrlr_BOG_Committee_Report {

    public BOG_Membership__c[] getOrgs() 
    {
        // Keep Unique MemTypes + record
        Map<String,BOG_Membership__c> uniqueRecords = new Map<String,BOG_Membership__c>();

        for(BOG_Membership__c bogm : [Select BOG_Membership_Type_New__c, ORG__c, contact__r.Name 
        FROM BOG_Membership__c 
        WHERE BOG_Membership_Status__c = 'Active' 
        AND BOG_Membership_Type_New__c  '' 
        ORDER BY BOG_Membership_Type_New__c , ORG__c, contact__r.Name ])
        {  

            uniqueRecords.put(bogm.BOG_Membership_Type_New__c+bogm.ORG__c,bogm);

        } 

        return uniqueRecords.values();

    }
}

The below lines

String[] memTypes = New List ();
        String[] orgs = New List ();    

        List repList = new List();
        for(BOG_Membership__c record :bogQueryList)
        {
            //memTypes.add((String) record.get('BOG_Membership_Type_New__c'));
            //orgs.add((String) record.get('ORG__c'));
            repList.add(new ReportWrapper((String) record.get('BOG_Membership_Type_New__c'),(String) record.get('ORG__c'),record.contact__r.Name));

        }

are useless as they are not doing anything accept adding field values in two separate lists from a single record. This can be done just by returning record. Instead directly return record and use on VF as:

<apex:repeat value="{!orgs}" var="BOGMembership" id="theRepeatId">

    <apex:outputText value="{!BOGMembership.BOG_Membership_Type_New__c}" /><br/>
    <apex:outputText value="{!BOGMembership.ORG__c}" /><br/>
    <apex:outputText value="{!BOGMembership.contact__r.Name}" /><br/>

</apex:repeat>
  • I like your shortening of code but 1) I get an error 'unknown property orgs; I replaced it with bogQueryList but same error 'unknown property bogQueryList ' what to do. Also, I needed the two list because I really want them as a Set to prevent duplicates. Why needs to be added so memtypes and orgs are unique? – Apex N-u-b Mar 17 '16 at 10:29
  • @ApexN-u-b have you set controller= vfCtrlr_BOG_Committee_Report for <apex:page>? – Ashwani Mar 17 '16 at 10:50
  • I did. Now it works. Many thanks. Is there a way to make it a set so there are not duplicates? – Apex N-u-b Mar 17 '16 at 10:55
  • May I push my luck as ask how to display in a table. I tried putting in <table> <tr> <td> tags but no go. Does it accept it and perhaps I made a type-o or is there a SF way to making the data a table? – Apex N-u-b Mar 17 '16 at 11:11
  • Instead of Apex repeat use <apex:pageblocktable>. I will recommend to read about this tag it is 99% similar to <apex:repeat>. @ApexN-u-b – Ashwani Mar 17 '16 at 11:15
0

//You can try it by creating a wrapper class and returning list of that wrapper //class in the getter method 'getOrgs()'

Edit: As all the values are from the same object you don't need a wrapper object.

Something like;


public class vfCtrlr_BOG_Committee_Report {

  public List<BOG_Membership__c> getOrgs() 
    {
        return [Select BOG_Membership_Type_New__c, ORG__c, contact__r.Name 
        FROM BOG_Membership__c 
        WHERE BOG_Membership_Status__c = 'Active' 
        AND BOG_Membership_Type_New__c  '' 
        ORDER BY BOG_Membership_Type_New__c , ORG__c, contact__r.Name ];
    }
}

And modify your VF like;


<apex:repeat value="{!orgs}" var="org" id="theRepeat">

    <apex:outputText value="{!org.BOG_Membership_Type_New__c}"/> &-nbsp;&-nbsp;&-nbsp;
    <apex:outputText value="{!org.ORG__c}"/> &-nbsp;&-nbsp;&-nbsp;
    <apex:outputText value="{!org.contact__r.Name}"/> </br>
</apex:repeat>

Note: use correct symbol for html space instead of &-nbsp;

  • 1
    just for a side note. in controller so many wrong syntax used. – Ratan Paul Mar 17 '16 at 9:26
  • "orgs" is a list of string with each instance being a string. So how does [string].memType etc work? – Prajith Mar 17 '16 at 9:26
  • can you point me those wrong syntax @sfdcweb? – Saumya Ranjan Satapathy Mar 17 '16 at 9:28
  • I have set orgs as List<ReportWrapper> type @Stygon. Check getOrgs( ) method in my controller. – Saumya Ranjan Satapathy Mar 17 '16 at 9:30
  • Fine. Got confused between, the "orgs" you have defined inside your "getOrgs" method. My humble apologies :) – Prajith Mar 17 '16 at 9:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.