[SalesForce] Assigning VF component attribute to a List controller class variable

I have a VF page using a standard controller, a component, and a custom controller for the component. I am trying to pass a list of sObjects from the standard controller to the component's custom controller. Currently, I have it set up as follows:

In VF page:

...   
 <c:ProjectSummaryComponent project="{!Project__c}" summaries="{!Project__c.Project_Summaries__r}" />
...

In VF component:

   <apex:component controller="ProjectSummaryController">
       <apex:attribute name="project" type="Project__c" required="true" description="The Project__c record" />
       <apex:attribute name="summaries" type="Project_Summary__c[]" assignTo="{!projectSummaries}" required="true" description="The list of Project Summaries." />
    ...

In controller:

public with sharing class ProjectSummaryController {
    public Project_Summary__c[] projectSummaries {get; set;}
...

I get no build errors from this code, but when viewing the page, I get this Visualforce error message:

Cannot convert the value of '{!projectSummaries}' to the expected type. 

I'm not sure what is wrong here, since the type is specified to be Project_Summary__c[] in both the component and the controller. From the component, I can actually display the list of Project_Summary__c records that were passed from the main page.

Any help would be appreciated. Thanks.

Best Answer

I confirmed this behavior in my own test and was able to work around it as follows:

VF component:

<c:ProjectSummaryComponent project="{!Project__c}" summaries="{!projectSummaries}" />

...

VF controller:

private Project__c  getProjWSummaries (){
                            return [select id, name, 
                                       (select id, name from Project_Summaries__r) 
                                     from Project__c where id = :this.project__c.id][0];}
public List<Project_Summary__c> projectSummaries {
   get {return getProjWSummaries().project_summaries__r;} private set; }  

VF component: no change

The problem has something to do with the relationship Project_Summaries__r if provided directly as you did in the OP when assigned to the component's controller. What exactly I do not know. As you can see from the code above, simply fetching the relationship in a property of List type works fine.

Related Topic