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
Ram ChaturvediRam Chaturvedi 

How can we use list<sobject> field value in visualforce page component <apex:pageblockTabel> ?

//class code :

string para = System.currentPageReference().getParameters().get('objName');
          
          String soql = 'select id, name from ' + para + ' limit 15 ' ;
          system.debug(soql);
           objs = Database.query(soql);
========================================
Visualforce page :
<apex:pageBlockTable var="st" value="{!objs}">
         
              <apex:column headervalue=" Name" >
                 
                          <apex:outputText value="{!st.name}"/>
                
              </apex:column>
         
  </apex:pageBlockTable>

This gives error : Unknown property 'SObject.name'
Best Answer chosen by Ram Chaturvedi
AshlekhAshlekh
Hi,

Use below code

opportunity
Visualforce page :
<apex:pageBlockTable var="st" value="{!objs}">
         
              <apex:column headervalue=" Name" >
                 
                          <apex:outputText value="{!st['nane']}"/>
                
              </apex:column>
         
  </apex:pageBlockTable>

All Answers

AshlekhAshlekh
Hi,

Use below code

opportunity
Visualforce page :
<apex:pageBlockTable var="st" value="{!objs}">
         
              <apex:column headervalue=" Name" >
                 
                          <apex:outputText value="{!st['nane']}"/>
                
              </apex:column>
         
  </apex:pageBlockTable>

This was selected as the best answer
Ram ChaturvediRam Chaturvedi
Thanks Ashlekh 

James LoghryJames Loghry
Note, if you have a concrete sobject, say Contact, Account, CustomObject__c then your Visualforce page should work.

Also, you don't need to hardcode the heading when outputting a field.  You can simply use <apex:column /> and let Salesforce / Visualforce figure out the header for you.  See below for an example:

<apex:pageBlockTable var="st" value="{!objs}">
        <apex:column value="{!st['Name']}" />
</apex:pageBlockTable>
Ram ChaturvediRam Chaturvedi
how can we print these name and id in class , in for each loop ;

List<sObject> objs ;
objs = Database.query('select name from Account');


                for(sObject sobj : objs){

                   system.debug(sobj['name']);           // this gives error Expression must be a list type: SObject
                }