This question already has an answer here:

Visualforce has initialization of visualforce components in apex. For example MyCustomComponent component: ApexPages.Component customComponent = new Component.c.MyCustomComponent();

Is that possible to pass dynamically the component name instead of using Component.c.MyCustomComponent? I want to build a method that can get component name dynamically from the custom setting and initialize new ApexPages.Component.

marked as duplicate by battery.cord, glls, Reshma, Pranay Jaiswal, Brian Mansfield Apr 17 at 17:07

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


This could require some dynamic Apex. Use Type.forName Apex Method to instantiate your component

Type componentType = 'Component.c.MyCustomComponent';
ApexPages.Component = (ApexPages.Component)componentType.newInstance();
  • did you mean: Type componentType = Type.forName('Component.c.MyCustomComponent'); ? If yes – it returns null in componentType. – Eugene Vabishchevich Apr 16 at 12:36
  • ok, this approach seems not working anymore. So probably as workaround you may put additional factory class in between that will have method to create desired component. Check this question from 2014 salesforce.stackexchange.com/questions/50295/… – pklochkov Apr 16 at 13:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.