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
neeruneeru 

problem with dynamically creation of pageblock table

Hi Everyone

 

      i came across a situation where i need to create pageblocktable  many times so i want to create a component which will create pageblock table dynamically i have this idea how to resolve it...

 

My Approch is

 

Here i am retrieving the object id from the url in visualforce page through object id i have the code to retrieve the object name and fields of that object dynamically...and also i retrieving the records through dynamic soql.....But i stuck How to Display These Records in PageBlockTable......

 

help me out in this scenario.

 

visualforce page:

<apex:page >
          <c:Dynamicpbt objId="{!$Currentpage.parameters.id}"/> 
</apex:page>

                 // here we get the object from url dynamically through objid....

 

Component:

 

<apex:component controller="dynamicpbt">
  <apex:attribute name="objId" type="String" description="The id of the object to which PBT for" required="true" assignTo="{!sobjId}"/>
   <apex:form id="PBTForm">
    <apex:pageBlock title="DYNAMICPBT">
    </apex:pageBlock>
   </apex:form>
</apex:component>
 
Controller Logic:
 
//this code retrives the fields name ,Object names  dynamically
 
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
Map<String,String> objectMap = new Map<String,String>();
for(Schema.SObjectType f : gd)
{
     objectMap.put(f.getDescribe().getKeyPrefix(), f.getDescribe().getName());
}
 
String sampleId ='00390000003LIVw';(//here we can pass objctid through objid)
String prefix =  sampleId.substring(0,3);
String objectName = objectMap.get(prefix);
System.debug('** SObject Name ** '+objectName);
 
Map<String, Schema.SObjectField> desResult = Schema.getGlobalDescribe().get(objectName).getDescribe().Fields.getMap();
List<String> fieldList = new List<String>();
fieldList.addAll(desResult.keySet());
for(integer i =0;i<fieldList.size();i++)
{
    System.debug('** Field Name ** '+fieldList[i]);
 
}
 
 
along these we can write a dynamic soql query to retrieve the records ...i'm stuck here to bind these records to component to disply in pageblock table
 
String soqlquery = 'Select '+fieldlist+' from '+objectname+' ';
        // Run the query
        queryresult = Database.query(soqlquery);
        return queryresult;
 
 
Thanks,
neeru
SammyComesHereSammyComesHere

You might want to make use of Apex:repeat for this. Not sure how compatible it is with apex:block .. Please try and let me know

 

   

<apex:repeat value="{!strings}" var="string" id="theRepeat">
<apex:pageBlock >
                <apex:pageBlockTable value="{!results}" var="c" rows="50" id="cases_table" >
                    <apex:column headerValue="Case Id">
                        <a target="_parent" href="/{!c.id}">{!c.CaseNumber}</a>
                        <apex:facet name="header">Case Number</apex:facet>
                    </apex:column>
                    <apex:column headerValue="Account Id ">
                        {!c.AccountId}
                    </apex:column>
                    <apex:column headerValue="Case Number">
                        {!c.CaseNumber}
                    </apex:column>
                </apex:pageBlockTable>
            </apex:pageBlock>


OR YOUR CMP
</apex:repeat>
neeruneeru

Thnx for reply sammy

 

even i dont know how it comes pageblock table dynamically  that is my scenario...i am unable to call the pageblock table in component..

SammyComesHereSammyComesHere

Use an Apex: repeat Tag. Anything you write in here would be repeated till its counter is not over.

 

<apex:repeat value="{!strings}" var="string" id="theRepeat">
My Code <div></div>
</apex:repeat>

  Here strings is a list with say size as 5. So anything written in apex:repeat would be repeated. In my case 5 divs would be created. Works like a for loop