function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
gtuerkgtuerk 

Using dynamic rendering with Apex:repeat - BUG

I have an order form that uses a collection of components that has content that is dynamically generated.  Each instance of the component checks a local variable to determine if certain content needs to be rendered.  I've noticed that once a 'false' is encountered for the component, all subsequent rendering of that component element is also rendered false.  So I skinnied this down in a different sandbox environment so that SF could have a 100% repeatable scenario to work from.

 

To prove this out, I added a custom object, embedded a VF page in that object's layout, added a controller extension that housed a member list collection (of a different wrapper class) and in the VF page I added an apex:repeat to iterate over the member collection.  Simple.  The Wrapper object takes a String when instantiated and there is a local boolean in the wrapper that returns true if the String passed to the Constructor is 'First'.  So here:

 

public class DynamicComponent {

Boolean isFirst;
public DynamicComponent(String dynamicType){
isFirst = false;

if (dynamicType == 'First'){
isFirst = true;
}
}

public Boolean getIsFirst(){
return isFirst;
}
}

 And in the page controller I instantiate 4 of these objects, 3 of which [0, 1 and 3 index] should return an isFirst of True:

 

public class DynamicController {

List<DynamicComponent> dynComps;
public DynamicController(ApexPages.StandardController controller){
DynamicComponent dynComp1 = new DynamicComponent('First');
dynComps = new List<DynamicComponent>();
dynComps.add(dynComp1);
DynamicComponent dynComp2 = new DynamicComponent('First');
dynComps.add(dynComp2);
DynamicComponent dynComp3 = new DynamicComponent('Second');
dynComps.add(dynComp3);
DynamicComponent dynComp4 = new DynamicComponent('First');
dynComps.add(dynComp4);
}
public List<DynamicComponent> getDynComps(){
return dynComps;
}
}

 

<apex:component >
    <apex:attribute name="EachElem" description="Each dynamic Element" type="DynamicComponent" required="false" />
    <apex:pageBlock title="Dynamic Element">
        <apex:pageBlockSection showHeader="false">
            <apex:pageBlockSectionItem rendered="{!EachElem.isFirst}">
                <apex:outputText value="I am rendered" />
            </apex:pageBlockSectionItem>
        </apex:pageBlockSection>
    </apex:pageBlock>
</apex:component>

 

And the VF Page, which iterates over the collection using an apex:repeat:

 

<apex:page standardController="Dynamic_Container__c" extensions="DynamicController" >
<apex:repeat value="{!dynComps}" var="eachComp">

<c:DynamicCollectionElement EachElem="{!eachComp}" />

</apex:repeat>

</apex:page>

So the expected result in this case would be 4 page blocks: first, second and fourth should have 'I am Rendered' output and the third should not.  But instead, the last one doesn't have the 'I am rendered' text.  

 

 DynamicElements

 

Also, if I change the first instantiated wrapper object to instead take a 'Second' value in the constructor, none of the component instances display the conditional text (leading me to believe the first false found causes subsequent rendering of the element to fail.  Please look into this, Visualforce friends

g

gtuerkgtuerk
Bueller?  Anyone?
Scott.MScott.M
I've run into this as well. I think they're doing some kind of property caching that causes the behavior.
gtuerkgtuerk

My workaround was to iterate over the view objects collection in my getter and pack everything that held true (didn't need to be hidden) in the zero index of a new collection.  that way, the things that needed to be hidden were all in the tail end of the collection.  This is a hokey workaround but was the only way to get the rendering to work correctly...

 

Thanks for corroborating

DevNVDevNV

Any update on this issue?  I'm having the same problem and actually had to create 2 separate lists and display 2 separate repeat segments to get the dynamic rendering working properly.  Would love to hear if Salesforce is working on this problem.

 

Niki

 

reatlimecoreatlimeco

I had a similar problem but what I found was the componenet was being called TWICE for each repeat.   I was running to the DML limits and was wondering why.   Salesforce told me that I couldn't depend on the component only being called once.

Scott.MScott.M

I've seen that behavoir as well, where the component is called twice. I can't figure out why they would do that, but it seems a bit unfair to count those multiple calls against your limit