You need to sign in to do that
Don't have an account?
Naznin 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>
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>
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
My advice would be for that method to return a User defined type, maybe an Inner Class that has the properties you need.
Thank you so much for a valuble suggetion.It helps me to improve my code.