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
AYUSH JAIN 29AYUSH JAIN 29 

Error: not found ListRec in VFPage pageBlockTable

<apex:page controller="dynamicObjectRecord_cntlr" >
    <apex:form >
        <apex:pageBlock title="Dynamic Object and it's Records">
            <apex:pageBlockSection title="SeleObject Information">
                <apex:inputText label="Object Name:" value="{!sobj}"/>
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockSection>
        
            <apex:pageBlockSection title="Records of {!sobj}">
                <apex:pageBlockTable value="{!listRec}" var="a">
                    <apex:column headerValue="ID" value="{!a.id}"/>
                    <apex:column headerValue="Name" value="{!a.name}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>


------------------------

public class dynamicObjectRecord_cntlr {
    Public sobject obj {get;set;}
    public String sobj { get; set; }
    public Map<string,Schema.sobjectType> mobj;
    public Set<String> s_allObj;
    public List<String> l_allObj;
    public List<sObject> listRec {get;set;} 
    public Boolean Opresent;
    
    public dynamicObjectRecord_cntlr ()
    {     obj = null;
        sobj= null;
        mobj = new Map<String,Schema.sobjectType>();
        s_allObj = new Set<String>();
         l_allObj = new List<String>();
         Opresent = false;
         listRec = new List<sobject>();
    }
    
     public PageReference search() {
        mobj= Schema.getGlobalDescribe();
        s_allobj = mobj.keySet(); 
        l_allObj.addAll(s_allobj);
         
         System.debug('Selected value is --'+sobj);
        for(String s:l_allObj)
        {
            if(s==sobj){
                Opresent=true;
                break;
            }
        }
        
         if(Opresent==true)
         {
             String query = 'SELECT id,Name from '+sobj +' LIMIT 50';
             System.debug('Query written is '+query);
             ListRec = Database.query(query);
             System.debug('List of Rec'+ListRec);
         }
        return null;
    }

}
Adarsh.SharmaAdarsh.Sharma
Hi Ayush,

Please try this:
<apex:page controller="dynamicObjectRecord_cntlr">
    <apex:form >
        <apex:pageBlock title="Dynamic Object and it's Records">
          <apex:pageBlockSection title="SeleObject Information">
                  <apex:inputText label="Object Name:" value="{!sobj}"/>
                <apex:commandButton value="Search" action="{!search}"/>
            </apex:pageBlockSection>
        
            <apex:pageBlockSection title="Records of {!sobj}">
                <apex:pageBlockTable value="{!listRec}" var="a">
                    <apex:column  value="{!a.id}"/>
                    <apex:column  value="{!a['name']}"/>
                </apex:pageBlockTable>
            </apex:pageBlockSection>  
        </apex:pageBlock>
    </apex:form>
</apex:page>
Hope this helps you!

If this helps you, please mark it as solved so that it will be available for others as a proper solution.

Thanks and Regards
 
AYUSH JAIN 29AYUSH JAIN 29
Thanks @SFDom . It helped.But Can you explain the logic behind it. Why we are writing like {!a['name']} ?
Adarsh.SharmaAdarsh.Sharma
Hi Ayush,
at this time sobject's type is map and your key is name.so we are doing this.

please mark above solution as best answer.if your problem solved.

thanks