0

Playing around with Dynamic Visualforce Components I found it inconvenient to create child elements in such a bulky notation

public Component.Apex.OutputPanel getNavPanel1(){
    Component.Apex.OutputPanel p = new Component.Apex.OutputPanel();
    p.childComponents.add( new Component.Apex.OutputText( value = 'test_dummy_text_1' ) );
    p.childComponents.add( new Component.Apex.OutputText( value = 'test_dummy_text_2' ) );
    p.childComponents.add( new Component.Apex.OutputText( value = 'test_dummy_text_3' ) );
    return p;
}

I would prefer a notation like that (which does not work):

public Component.Apex.OutputPanel getNavPanel2(){
    return new Component.Apex.OutputPanel( childComponents = new ApexPages.Component[]{
         new Component.Apex.OutputText( value = 'test_dummy_text_1' ) 
        ,new Component.Apex.OutputText( value = 'test_dummy_text_2' ) 
        ,new Component.Apex.OutputText( value = 'test_dummy_text_3' ) 
    });
}

Looking at the documentation here https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_pages_dynamic_components.htm#apex_ApexPages_Component_childComponents it should be possible because it's defined as

public List <ApexPages.Component> childComponents {get; set;}

Any ideas why this does not work?

If we add at getNavPanel1() a line like this before the return

p.childComponents = new ApexPages.Component[]{}

we get at least a hint form the compiler: "Variable is not visible: childComponents" - but why? If it's a public property as defined, it should be writable. Or do I get this wrong?

1 Answer 1

1

Only SObjects support named constructor parameters so this won't work:

return new Component.Apex.OutputPanel(childComponents = new ApexPages.Component[] {...});

This not working I think means the documentation is wrong and its actually private set:

Component.Apex.OutputPanel p = new Component.Apex.OutputPanel();
p.childComponents = new ApexPages.Component[] {...});

So that just leaves this which is probably not what you want but does compile:

Component.Apex.OutputPanel p = new Component.Apex.OutputPanel();
p.childComponents.addAll(new ApexPages.Component[] {...});

Otherwise I guess you'll have to create your own factory methods or builder class.

PS

I've been put off using these classes because of this problem: Field Values In Dynamic Component Not Retaining After Error.

1
  • Thanks @KeithC for the backgrounds. Agreed: the docu seems to be wrong. Bud why the heck they made it private. That "after Error issue" is really bad and it's hard to follow the by-design statement... Actually (I wondered myself) the named constructors are supported and this works new Component.Apex.OutputText(value = 'test_dummy_text_1').
    – Uwe Heim
    Commented Aug 25, 2014 at 22:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .