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
Naznin VhoraNaznin Vhora 

Error: Unknown property 'SObject.Name__c'

Hello everyone,
As I am newbie to salesforce, I am trying to explore and learn various concept of it. Today I am trying to learn dynamic SOQL Concept, but I am facing one error in it.
Here, I take input (Object Name) from the end user, and fetch the information from that specific object. Since the user is going to enter the object name, I don’t want to hardcode the Object name in the apex class. That is why I have used generic sobject type named “sObject” for creating a List variable but on Visual force page I got the following error:
Error: Unknown property 'SObject.Name__c'  
I understand the error which says that Name__c is the property of my custom object(Student__c),and I have created the object type as generic that’s why it throws the error message.
I have tried various solution such as casting generic sObject to specific custom object,but still my problem is unsolved.
Can anyone please help me to tackle this problem,It will help me to understand this new concept very well.

Please check my code snippet.

Apex Class : 

public class Dynamic_SOQL
{
    public String name {get;set;}    
    public List<sObject> sobj=new List<sObject>(); 
         
    public void searchresult()
    {
        
        String qry='select id,Name__c from '+name;
        sobj=Database.query(qry);           
        
        ApexPages.Message msg=New ApexPages.Message(ApexPages.Severity.INFO,name);
        ApexPages.addMessage(msg);       
       
    }
    public List<sObject> getresult()
    {       
        return sobj;
    }
}

Visual force Page : 

<apex:page docType="html-5.0" controller="Dynamic_SOQL">
<apex:form >
<apex:pageBlock >
    <apex:pageBlockSection >
        
        <apex:inputText label="Object Name" value="{!name}"/>
        <apex:commandButton value="Search" onclick="searchresult()" reRender="mymsg"/>
        <apex:pageMessages id="mymsg"></apex:pageMessages>
        
        <apex:pageBlockTable value="{!result}" var="r">
            <apex:column value="{!r.id}"/>
            <apex:column value="{!r.Name__c}"/>
        </apex:pageBlockTable>
        
        <apex:actionFunction action="{!searchresult}" name="searchresult"/>
        
        
    </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Steven NsubugaSteven Nsubuga
The Name field is a standard field. It is always "Name" unless you have created a Custom field called "Name". Change the query to this:
String qry='select id,Name from '+name;

 
Naznin VhoraNaznin Vhora
Hi Steven Nsubuga,
I have changed my query as per your suggestion,but still it throwing me a same compile time error,but still it gives a correct output
 
Steven NsubugaSteven Nsubuga
You also need to make the change in the visualforce page.
<apex:pageBlockTable value="{!result}" var="r">
            <apex:column value="{!r.id}"/>
            <apex:column value="{!r.Name}"/>
 </apex:pageBlockTable>

 
Naznin VhoraNaznin Vhora
Yes , I did that when I changed the query,but still same error.
User-added image
Steven NsubugaSteven Nsubuga
Ok, the problem is that the getresult method in the controller returns the generic sobject, rather than a specific object like Account. I know that you want to be dynamic and be able to return all sobjects but for now that is the reason for the error.
My advice would be for that method to return a User defined type, maybe an Inner Class that has the properties you need.
Steven NsubugaSteven Nsubuga
Also, design wise, it is better to present the user with a drop down list of the available sobjects, rather than asking the user to type in a name.
 
Naznin VhoraNaznin Vhora
Hi Steven Nsubuga,
Thank you so much for a valuble suggetion.It helps me to improve my code.