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
nimbusprojectnimbusproject 

How to make a visualforce table or datalist from a List<List<String>> of field values

I am wondering how to do the following which should be easy but I have yet to get anything but a datalist of the same value which is the last value in the List<List<String>>

 

Here is my visual force and controller methods

 

<apex:dataList value="{!FieldValues}" var="reports"> <apex:repeat value="{!reports}" var="fieldvals"> <apex:outputText value="{!fieldvals}" /> </apex:repeat> </apex:dataList>

 

public List<List<String>> getFieldValues(){ List<List<String>> returnVal = new List<List<String>>(); List<String> fieldRow = new List<String>(); for(integer i = 0; i<finalObjects.size(); i++){ fieldRow.clear(); for(String related : relatedLabels){ fieldRow.add((String)finalObjects[i].get(related)); } returnVal.add(fieldRow); } return returnVal; }

 

finalObjects is just a List<sObject> with a list of different sObjects, such as Accounts and Contacts

 

relatedLabels is just a List<String> that is a list of string names of the fields that Accounts and Contacts have in common.

 

I know that finalObjects and relatedLabels have the correct values since I have tested them with many other methods that my reporting tools is using.

 

Basically I just need to know if I am doing something incorrect or how to go about iterating over a List<List<String>> in some manner to at least display the values in a table, datalist, or just output them to the visualforce page.

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is because you are re-using the fieldRow list within your loop - each element in your list will contain a reference to the same fieldRow list.  The fieldRow list will only contain the last set of data from finalObjects as you clear and repopulate it each time through the loop.

 

Once you have added fieldRow to the returnVal, you need to instantiate a new list, e.g.

 

 returnVal.add(fieldRow);

fieldRow = new List<String>(); 

 

You can then remove the fieldRow.clear() line, as you are using a freshly created empty list each time.; 

All Answers

bob_buzzardbob_buzzard

This is because you are re-using the fieldRow list within your loop - each element in your list will contain a reference to the same fieldRow list.  The fieldRow list will only contain the last set of data from finalObjects as you clear and repopulate it each time through the loop.

 

Once you have added fieldRow to the returnVal, you need to instantiate a new list, e.g.

 

 returnVal.add(fieldRow);

fieldRow = new List<String>(); 

 

You can then remove the fieldRow.clear() line, as you are using a freshly created empty list each time.; 

This was selected as the best answer
nimbusprojectnimbusproject

This worked!

Thanks a ton, you just saved me from getting a headache.

p999_dfsp999_dfs

Can you please share the code, how to pupulate values in

 

 

finalObjects is just a List<sObject> with a list of different sObjects, such as Accounts and Contacts

 

relatedLabels is just a List<String> that is a list of string names of the fields that Accounts and Contacts have in common.

 

Thanks