3

I am setting a string as an attribute someString in a custom component tabComponent from the mainPage. I know that we cannot access the attribute in the constructor of the VF component controller, but we can access the attributes in a getter method. Now, I am able to access the attribute in methods which are returning String or Integer, but the attribute is coming as null if I access it from the method returning a complex type like Component.Apex.OutputPanel.

Main VF page:

<apex:page>
    <c:tabComponent someString="panelId"/>
</apex:page>

VF Component:

<apex:component controller="ComponentClass">
    <apex:attribute type="String" description="Some String" name="someString" assignTo="{!someAttribute}"/>
    {!theString}<br/> //This prints panelId
    {!someInteger} //This prints 20
    <apex:dynamicComponent componentValue="{!Panel}"/> //This renders a Panel with the Id=somePanelnull. Which is not correct.

</apex:component>

VF Component Controller:

public class ComponentClass {
    public String someAttribute {get;set;}

    //this method is where my problem is
    public Component.Apex.OutputPanel getPanel() {
        system.debug('the value of the string '+ someAttribute); //this always returns null
        Component.Apex.OutputPanel outputPanel = new Component.Apex.OutputPanel();
        outputPanel.Id = 'somePanel'+someAttribute;
        return outputPanel;
    }

    //I can access the attribute value here.
    public String gettheString() {
        return someAttribute;
    }

    //I can access the attribute value here as well
    public Integer getsomeInteger() {

        system.debug('the value of the String '+ someAttribute);
        return 20;
    }
}

Need urgent help to resolve this.

  • In the VF component you have 'theString' and 'someInteger' that don't appear in the controller. Should one of these be 'someAttribute'? – Daniel Ballinger Dec 28 '14 at 18:19
  • These are the name of the methods in the controller. – Sam Dec 28 '14 at 23:54
  • Ah, I see the getX methods now. My fault for checking the question on a cellphone. Maybe try putting system.debug statements in the someAttribute get and set bodies. You could also give it a default value in a constructor. Then check the order of the debug statements in the log. – Daniel Ballinger Dec 29 '14 at 17:16
1

I agree that this behavior is not analogous to the pattern. Its evaluating differently when it comes to dynamic components. This issue is being faced by many users.

Dymamic components execution order issue

I think currently this can help you out :

public class ComponentClass {
    public String someAttribute {get;set;}
     public Component.Apex.OutputPanel Panel {get;set;}

    //this method is where my problem is
    public Component.Apex.OutputPanel evaulatePanel() {
        system.debug('the value of the string '+ someAttribute); //this always returns null
        Component.Apex.OutputPanel outputPanel = new Component.Apex.OutputPanel();
        outputPanel.Id = 'somePanel'+someAttribute;
        return outputPanel;
    }

    //I can access the attribute value here.
    public String gettheString() {
        Panel  = evaulatePanel();
        return someAttribute;
    }
    public ComponentClass(){

    }
    //I can access the attribute value here as well
    public Integer getsomeInteger() {

        system.debug('the value of the String '+ someAttribute);
        return 20;
    }
}

Also one of the temporary workaround could be to re-render the page after first page load.

  • Thanks Amit. I have already done this. But I wanted to know why is it not working in those getter method. – Sam Dec 29 '14 at 14:02
  • 1
    At this time , we can just assume that the getters for dynamic components evaluate differently. I cannot see any documentation around it. I shall get back if I find anything concrete!@ – Amit Bangad Jan 2 '15 at 6:19
  • I have modified the answer with some more research! – Amit Bangad Jan 2 '15 at 12:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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