Discussions
- General Development
- Schema Development
- Apex Code Development
- Visualforce Development
- Formulas & Validation Rules
- Security
- Mobile
- Force.com Sites & Site.com
- Chatter Development
- Java Development
- .NET Development
- Perl, PHP, Python & Ruby
- Desktop Integration
- APIs and Integrations
- Visual Workflow
- Apple, Mac and OS X
- VB and Office Development
- AppExchange Directory & Packaging
- Salesforce Labs & Open Source Projects
- Other Salesforce Applications
- Jobs Board
- Force.com Discussion Boards
- :
- Developer Boards for Force.com and Database.com
- :
- Apex Code Development
- :
- Re: Dynamic Component Problem with TypeMethods
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic to the Top
- Bookmark
- Subscribe
- Printer Friendly Page
Dynamic Component Problem with TypeMethod s
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 12:11 PM
Hello everyone,
I created this post because I have a problem. I want to create a dynamic component to add it to a page. So here is my configuration :
TabLayoutLead
<apex:page id="tabLayout" >
...
<c:myDynamicComponent compToAdd="QuoteComponent" contAssoc="QuoteComponentController" extName="ClassExtensionName"/>
...
</apex:page>
MyDynamicComponent
<apex:component controller="MyDynamicController" >
<apex:attribute name="compToAdd" description="The component to dynamically add to the stage" type="String" required="true" assignTo="{!compToAdd}" />
<apex:attribute name="contAssoc" description="The controller associated to the component" type="String" required="true" assignTo="{!contAssoc}" />
<apex:attribute name="extName" description="The extension label associated to the compoonent to get the regular or custom component" type="String" required="true" assignTo="{!extName}" />
<apex:dynamicComponent id="theParent" rendered="false" componentValue="{!addComponent}"/>
</apex:component>
MyDynamicController
global class MyDynamicController {
public Type t { get; set; }
public String compToAdd { get; set; }
public String contAssoc { get; set; }
public String extName { get; set; }
public MyDynamicController()
{
}
public ApexPages.Component getAddComponent()
{
if (extName != 'Default')
compToAdd = compToAdd + extName;
t = Type.forName(compToAdd);
return (ApexPages.Component) t.newInstance();
}
}
My final goal is, having a list of custom components, to be able to create them on my page by just calling one single line. So in MyDynamicComponent, I create the new component using the apex:dynamicComponent. But I have a problem, because in my controller, the function called dynamicComponent.getAddComponent(), when creating a newInstance using TypeMethods, throws a NullPointerException because t = Type.forName(compToAdd) returns null. I've figured out that TypeMethods are for Classes only and not Components. Then I wanted to create a Class which extends ApexPages.Component so I can use it with the TypeMethods but I then got the following error "Error: Compile Error: Parent class has no 0-argument constructor for implicit construction".
So I wanted to know, can I technically do what I want to do ? I come from a Flex/Java world so maybe I'm at the limit of Salesforce.
If I can do it, do you have any suggestions for me please, maybe you already did this.
Thank you very much.
Re: Dynamic Component Problem with TypeMethod s
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 12:33 PM
You can create a wrapper class/Interface. Here's one possible solution:
// Just need 1 of these.
global Interface ComponentReference { ApexPages.Component getInstance(); }
// Need 1 per component.
public class MyComponent1Reference implements DynamicComponentReference { ApexPages.Component getInstance() { return new Component.mynamespace.MyComponent1(); } }
public void dosomething() {
Type t = Type.forName('mynamespace','MyComponent1Reference' );
ApexPages.Component cr = t.newInstance().getInstance();
}
The value of this is fairly limited in scope; you need a way to configure elements, and because of the nature of binding, this might blow up later.
Your Flex background has served you well; regrettably, Apex Code is significantly more restricted in terms of dynamic types than Flex is.
~ sfdcfox ~
I am a sandwich. That is all.
Re: Dynamic Component Problem with TypeMethod s
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-05-2013 01:07 PM
Re: Dynamic Component Problem with TypeMethod s
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 07:54 AM
Thanks. Your solution works very well. To be a little bit more tricky, what I did is that in my controller I added this getInstance function so I dont need to create any more classes.
Now I have one more question. Apparently, the dynamicComponent is added to the Page between between controller's constructor and variable assignment. Which means that, in my dynamicComponent adding, I cannot used fields and attributes that I need to customize my component.
Do you know any way to get these attributes before the dynamicComponent's componentValue is called please ?
Thank you.
Re: Dynamic Component Problem with TypeMethod s
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Highlight
- Email to a Friend
- Report Inappropriate Content
02-06-2013 06:31 PM
Getters are called after setters, so you should be able to see the values and attributes you have. Just remember to not set any variables in a getter method, and do not depend on any values being available in setter methods. If you're still having problems, maybe you could post an example?
~ sfdcfox ~
I am a sandwich. That is all.

