+ Start a Discussion
jayamjayam 

Accessing list<sobject> in vf page

 

hi,

        I got an error named 'Unknown property 'SObject.name' 'when Accessing the list<sobject > in my vf page .

 

Here is my code .

 

in Apex class

 

list<sobject> get names(){

list<sobject> objlist=[select name from Contact];

return objlist;

}

 

in vf page

<apex:datatable value="{!names}"  var="a" >

<apex:column>{!a.name}</apex:column>

</apex:datatable>

 

 

 what can I do?

 

thanks........

 

 

 

 

Vanessa BarrosVanessa Barros

hi!

 

must be like this "public list<sobject> getnames()"

Prajapati.LakhanPrajapati.Lakhan

Hi, object properties are evaluated at compile time and i dont think sobject support "name" property you have to type cast it in contact, prior to bind with visualforce. If you must have to use sobject then you can use inner classes to associate generic properties.

 

 

 

Thanks,

Lakhan

jayamjayam

 Hi 

 

Thanks for your reply lakhan

 

But now I have to know how to use innner classes .

 

if u know you can send  example to me.

 

thanksssssss

 

 

 

Prajapati.LakhanPrajapati.Lakhan

Hi Jayam,

 

I am uncertain about why do you want the output of (concrete sObject) contact in List<sObject>, you could simply use List<Contact> then there would not be any problem related to unknown property.

 

// Apex class

 

List<Contact> getNames(){

list<Contact> objlist=[select name from Contact];

return objlist;

}

 

// vf page

<apex:datatable value="{!names}"  var="a" >

<apex:column>{!a.name}</apex:column>

</apex:datatable>

 

 

 

However for your reference if you have to use dynamic query where type of sObject is uncertain it can be handled in other way like-

 

 

string dynamicSObject = 'Contact';
list<sobjectWrapper> getNames(){
List<sObjectWrapper> sObjWrapList = new List<sObjectWrapper>();
list<sobject> objlist= Database.query('Select id, name From '+ dynamicSObject);
for(sObject sobj: objList) {
   sObjectWrapper sobjWrap = new sObjectWrapper();
   sObjWrap.id = sObj.get('id')+'';
   sObjWrap.name = sObj.get('name')+'';
   sObjWrapList.add(sObjWrap);
}
return sObjWrapList;
}

//Inner class because of we would be interested in common field of all sobjects
public class sObjectWrapper {
  public string id{get;set;}
  public string name{get;set;}
}


in vf page

<apex:datatable value="{!names}"  var="a" >

<apex:column>{!a.name}</apex:column>

</apex:datatable>

 

 

 

 

Thanks,

Lakhan

jayamjayam

Thanks Lakhan ,

 

I Solved my Problem ,

 

Thank you very much Lakhan .....