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
dai tran 6dai tran 6 

Can i use sObject in visualforce page?

Controller:
public List<sObject> MyProducts {get;set;}
 public void search(){ 
           string searchquery='select ProductCode,Name,ImageName__c,(Select 
          UnitPrice,Pricebook2.Name From PricebookEntries where IsActive=True)  from Product2 where name like \'%'+searchstring+'%\' Limit 20'; 
            MyProducts= Database.query(searchquery); 
}
visualforce page:
<apex:repeat value="{!MyProducts}" var="row">
                    <div >
                       <apex:image width="100p" height="100p" value="{!URLFOR($Resource.ProductImage, 'ProductImage/' & Value(row.ImageName__c))}"></apex:image>                 
                       <p>
                           <apex:outputText >{!row.Name}</apex:outputText>
                           <apex:repeat value="{!row.PricebookEntries}" var="PriceItem">
                                <apex:outputText >{!PriceItem.UnitPrice}</apex:outputText>
                           </apex:repeat>
                        </p>                                             
                    </div>
                </apex:repeat>

Can i use sObject in visualforce page?
 
Best Answer chosen by dai tran 6
Shruti SShruti S
I think all you have to do is change 
public List<sObject> MyProducts {get;set;}
to 
public List<Product2> MyProducts {get;set;}

All Answers

SandhyaSandhya (Salesforce Developers) 
Hi,

You can check below links for similar issue.

https://stackoverflow.com/questions/7335540/sobject-in-visualforce
 
https://salesforce.stackexchange.com/questions/38831/using-a-generic-sobject-in-visualforce-page/38836
 
Please mark it as solved if my reply was helpful, it will make it available
for others as a proper solution.

Best Regards,
​Sandhya
 
dai tran 6dai tran 6
Current it occur error: Unknown property 'SObject.ImageName__c'
Shruti SShruti S
I think all you have to do is change 
public List<sObject> MyProducts {get;set;}
to 
public List<Product2> MyProducts {get;set;}
This was selected as the best answer