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
Dolly BasantaniDolly Basantani 

Accessing Wrapper Class instance as an Attribute to custom components

Hi,

I am trying to pass wrapper class instance as an attribute to my custom component but it is not allowing me to do so as the "type" attribute in <apex:attribute> tag is expecting sObject/Class/primitive type.

Can anyone help me on this?
 
pconpcon
You have a couple of options for what you can do here.

You can re-build your data inside your component controller.
PROS: Super easy to do (most of the time) based on parameters such as Id
CONS: Increased SOQL queries can make it easy to run afoul of the governor limits

JSON serialize your data, pass it in as a string, deserialize the data in the controller
PROS: Your data stays intact and it's pretty fast (faster than doing SOQL)
CONS: Can make view state issues if the data is large.  Some of the data cannot be correctly serialized/deserialized depending on the object type/structure.

Static variable to store the data on the component
PROD: Even faster then serialization
CONS: Data can get stale easy and it requires that you populate it before you use your component.
Ben EdwardsBen Edwards
This is possible to do, however your wrapper class needs to be a top-level class, rather than an inner class. That is, the wrapper class needs to exist as its own file, rather than live inside another class. So this should work:
public class MyWrapperClass {
    public String myString {get;set;}
}


And then in your component:​

<apex:attribute name="wrapperInstance" type="MyWrapperClass" description="Instance of the wrapper class" />