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.


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 '19 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 '19 at 13:50

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