+ Start a Discussion
Thomas FullerThomas Fuller 

Apex/Visualforce: How do you modify a collection before it is iterated into a VF page?

Hey everybody,

I am currently working on a Visualforce component where it will display a filtered related list on the main page layout of the "Projects" ("PE_Projects__c") custom object. I've looked up various sources on how to achieve a filtered related list, and I've created a rough bit of code to test. However, everytime I load a Projects page, an error message appears in the Visualforce window saying: "Content cannot be displayed: Cannot modify a collection while it is being iterated." After researching this error a bit, I've tried using the addAll() method to copy a finished list to the "projConList" list. However, I'm still back at square one.


Visualforce Code:
<apex:page standardController="PE_Projects__c" extensions="ProjectRolesMainClass">
     <apex:form>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!projConList}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.Project_Contacts_con__c}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    </apex:form>
</apex:page>

Apex Code:
public class ProjectRolesMainClass {
    public List<Project_Contacts__c> projConList{get;set;}
    public ProjectRolesMainClass(ApexPages.StandardController controller) {
        List<Project_Contacts__c> tempList=[select Id, Project_Contacts_con__c from Project_Contacts__c where Project_Contacts_proj__c=:ApexPages.currentPage().getParameters().get('id')];
        for(Project_Contacts__c PR:tempList) {
			tempList.add(PR);
        }
        projConList.addAll(tempList);
    }
}


I was hoping some experienced VF developers would mind helping a newbie out by providing any suggestions, ideas, solutions, etc.

Thanks so much for your time.
Best Answer chosen by Thomas Fuller
Jim JamJim Jam
Not sure why you're using a for loop at all. Why don't you just populate the projConList direct from the query? something like ..

projConList = [select Id, Name, Project_Contacts_con__c from Project_Contacts__c where Project_Contacts_proj__c = :ApexPages.currentPage().getParameters().get('id')] ;

that should be the only line you need in the constructor method.

All Answers

Jim JamJim Jam
Not sure why you're using a for loop at all. Why don't you just populate the projConList direct from the query? something like ..

projConList = [select Id, Name, Project_Contacts_con__c from Project_Contacts__c where Project_Contacts_proj__c = :ApexPages.currentPage().getParameters().get('id')] ;

that should be the only line you need in the constructor method.
This was selected as the best answer
Thomas FullerThomas Fuller
Nice! Very simple solution! In my research, I found a statment similar to your's, but they included a for loop for some reason. I may have to go back to see what they did. I see your point, though. It's much easier to just do it directly, instead of building it inside a loop. Thanks so much!