function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
thornkthornk 

Attempting to build VF page dynamically

We have a UserID object which has around 200 picklists on it.  The User ID is tied to an Order Request.  I have a requirement to create a printout which displays all of the User IDs associated with a user request.  I can build the vf page and hard code every field, but that would be a nightmare to maintain.  I would like to create the page dynamically so that if we add new picklists in the future I don't have to go back and edit visualforce pages.  

 

My idea was to create a custom class which held the UserID, a list of fields and a list of values and then render them on the VF page using nested <apex:repeat> tags.  the outer repeat would loop through all of the UserIDs while the inner repeat would loop through the list of fields that I want to dynamically create.

 

I have a class that returns a map of field names and the getDescribe on those fields:

 

public with sharing class getUserIDSchema {
public static Map<String, DescribeFieldResult> fullMapping (){
Map<String, DescribeFieldResult> m = new Map<String, DescribeFieldResult>();
Map<String, Schema.SObjectField> uliFields = Schema.SObjectType.Townsend_User_Login_ID__c.fields.getMap();


for(String s : uliFields.keySet()){
if(s.substring(0,1) == 'X' ||
s == 'SCB_SWX_SCOACH_BASIC__c' ||
s == 'SCA_SWX_SCOACH_ADVANCED__c' ||
s == 'LSQ_LSE_INTL_L1_NEWS__c' ||
s == 'LSP_LSE_DOM_L1_NEWS__c' )
{
m.put(s,uliFields.get(s).getDescribe());
}
}
return m;
}
}

 

  I then create two Lists in my wrapper class, one is a list of strings holding the field Labels, the second list holding the Field value (at least I think it does):

public class wPrintID{
public Townsend_User_Login_ID__c tulid {get; set;}
public List<String> fields = new List<String>();
public List<Object> values = new List<Object>();
public wPrintID (Townsend_User_Login_ID__c a, Map<String, DescribeFieldResult> fMap){
tulid = a;
for(String s : getFields()){
if(fMap.isEmpty() <> false){
fields.add(fMap.get(s).getLabel()); //This is the list of field labels
values.add(tulid.get((fMap.get(s).getSObjectField()))); //This is the list of field values
}
}
}
}

 

I was hoping to render this out using something along the lines of the code below using the first loop to print out some basic info on the ID and the second loop to output the list of fields (and eventually the values):

 

<apex:page controller="printIDscontroller" showHeader="false" sidebar="false" renderas="pdf" title="User IDs">
<apex:repeat value="{!printIDs}" var="i" id="table">
<apex:repeat value="{!i}" var="f" id="fields">
<apex:outputText value="{!f.fields}"/>
</apex:repeat>
</apex:repeat>
</apex:page>

 

I think I ran into a wall here though and am unsure about how to get both the Fields and values printed out since the second repeat loop would need to be seeded with !i.fields to iterate over the list, but then I can't access !i.values.

 

I think I need to start over with a new approach, but I am not quite sure what direction to take.  Any advice, ideas, direction, etc. would be greatly appreciated at this point.

 

 

 


 

Message Edited by thornk on 01-28-2010 12:56 PM
Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

If you need to be able to extract values from multiple associated lists, the normal approach would be to hold a single list of wrapper classes containing all the associated information.

 

In this case:

 

Create a wrapper class that can hold a field label and value.

 

Replace your two lists with a list of this wrapper class. 

 

Rather than iterating the fields and populating entries in two lists, you would create a new instance of the wrapper class and store the label + value in it, and then add it to the list.

 

Finally, change your VF page so that the second repeat is iterating the list of wrapper classes, and output the label/value of each element in the list.

All Answers

stephanstephan

Hey thornk --

 

Currently the approach you've described won't work. We are, however, planning to address this in a future release. I can't make any promises in terms of a timeline, but this use case is something a number of partners and users have requested as well, so it's high on our list.

 

...stephan

bob_buzzardbob_buzzard

If you need to be able to extract values from multiple associated lists, the normal approach would be to hold a single list of wrapper classes containing all the associated information.

 

In this case:

 

Create a wrapper class that can hold a field label and value.

 

Replace your two lists with a list of this wrapper class. 

 

Rather than iterating the fields and populating entries in two lists, you would create a new instance of the wrapper class and store the label + value in it, and then add it to the list.

 

Finally, change your VF page so that the second repeat is iterating the list of wrapper classes, and output the label/value of each element in the list.

This was selected as the best answer
thornkthornk

Thank you bob and Stephan for your responses. 

 

Bob, I will try to implement what you have laid out.  I think I was moving in the right direction, just hadn't gotten there yet!!