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
arishi0arishi0 

Dynamically create page sections from custom object

Hello,

 

I have added a custom HTML component in the home page with a reference to a  visualforce page I have. The FrontPageNews  object will contain the info I want to show in the page blocks. It needs to be one page block per record. 

I need to iterate over the records and for each one, create a page block with the block title taken from a field in the custom object, and the page section has an outputText tag that would also get the text from a field in the custom object.

 

How can this be achieved? I have no clue how to iterate over objects in visualforce. Much lessdynamically create pageblocks and pageBlockSections.

 

Best Answer chosen by Admin (Salesforce Developers) 
sinsiterrulezsinsiterrulez

Hi,

In your VF code:

 

<apex:repeat value="{!FrontPageNewsItem__c}" var="item"> 

 

 

the attribute value in apex:repeat tag is not returning any value. Its null so no pageblocksection is getting created.

I suggest you create a method in extension class which will pass list of FrontPageNewsItem__c.

Using std controller will not automatically send you list of records of that object. You will need a method to explicitly pass it.

 

If it works as suggested, then please accept it as solution!!!!

All Answers

sinsiterrulezsinsiterrulez

Hi,

Try using <apex:repeat> tag in which add the pageblock thing

 

    <apex:repeat value="{!Items}" var="item">
        <apex:pageBlock title="{!item.name}">
        </apex:pageBlock>    
    </apex:repeat>

 

 

here Item is list of records of an object.

Let me know if you have any issues

arishi0arishi0

Hello,

 

Thank you for that piece of code it seems like the right solution, yet Im thinking that I might still be doing something wrong since when I save the page with teh code below, it refreshes empty, the pageblock is created but the pagesections are not, yet if I replace the {!item} variables with regular text it works. PLease let me know if you can spot my mistake.

 

 

 

<apex:page sidebar="False" showHeader="False"  standardcontroller="FrontPageNewsItem__c" >
 <apex:pageBlock >
<apex:repeat value="{!FrontPageNewsItem__c}" var="item">
        <apex:pageBlockSection title="{!item.Name}" >
         <apex:outputText value="{!item.Body_Text__c}" > </apex:outputText> 
         </apex:pageBlockSection>
        </apex:repeat>
</apex:pageBlock>
  </apex:page>

 

<apex:page sidebar="False" showHeader="False"  standardcontroller="FrontPageNewsItem__c" > 

<apex:pageBlock >

<apex:repeat value="{!FrontPageNewsItem__c}" var="item"> 

       <apex:pageBlockSection title="{!item.Name}" >        

          <apex:outputText value="{!item.Body_Text__c}" > </apex:outputText>                          </apex:pageBlockSection>      

 </apex:repeat>

</apex:pageBlock>  

</apex:page>

 

Thank you!

sinsiterrulezsinsiterrulez

Hi,

In your VF code:

 

<apex:repeat value="{!FrontPageNewsItem__c}" var="item"> 

 

 

the attribute value in apex:repeat tag is not returning any value. Its null so no pageblocksection is getting created.

I suggest you create a method in extension class which will pass list of FrontPageNewsItem__c.

Using std controller will not automatically send you list of records of that object. You will need a method to explicitly pass it.

 

If it works as suggested, then please accept it as solution!!!!

This was selected as the best answer