4

I'm trying to build a VF component that references an attribute that is an instance of a child class. I have a top level class A and inner class B and C which extend class A and i am trying to pass the visualforce component instance of class B. Since only top level classes can be passed to component i have declared the attribute as of type A and i am passing instance of class b to the component. But i am getting this error "Cannot convert the value of '{!classA}' to the expected type. " My question is can we pass child instances to component.

Visualforce Component

<apex:component controller="ComponentController">

    <apex:attribute name="list" 
        type="SObjectWrapper[]" 
        required="true"
        assignTo="{!sobjectWrappers}"
        description="" />

      <!--Some Code-->
</apex:component>

Component Controller

public with sharing class ComponentController{
    public List<SObjectWrapper> sobjectWrappers {
        get;
        set; 
    }
}

Visualforce Page

<apex:page controller="DemoController">
    <apex:form >
        <apex:repeat value="{!mapWrapper}" var="item">
          <c:DynamicTable list="{!mapWrapper[item]}" />
        </apex:repeat>
    </apex:form>
</apex:page>

Page Controller

public with sharing class DemoController {

    public Map<ParentNode,List<LevelOneNode>> mapWrapper{
        get ;
        set ;
    }
  // Some Code which populates the list
}

Base Class

public abstract class SObjectWrapper implements Comparable {
    // Some Code
}

Child Class

public class ParentNode extends SObjectWrapper {
    public List<LevelOneNode> levelOneNodes {
           get;
           set;
   }
   // Some Code
}

public class LevelOneNode extends SObjectWrapper {
         // Some Code
}

Also, if i directly pass instance of an child list the code is working, so the issue seems to be coming while iterating over map.

  • If B extends A and if B is declared as a top level class, then you can definitely pass to the component. Inner classes are more problematic - see salesforce.stackexchange.com/questions/15872/… – cropredy Oct 8 '14 at 17:05
  • i refactored my inner class to top level class but the same issue is occurring. – anmrk Oct 8 '14 at 17:52
  • Hmm - I have made this work so you should post the relevant component, invoking page, controller property, base class, and extension class bits of your issue to your original post - strip out all but the most essential bits so as not to 'code dump' on us :-) – cropredy Oct 8 '14 at 18:11
2

No you can't pass the child instance to a component attribute. We also ran into this same issue some time back and found the only solution is to declare the child instance class as a separate standalone class.

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.