+ Start a Discussion
saykosayko 

How do I make a SOQL query from a custom object data using apex?

I have a custom object Students and several fields within it. I now wish to access that data using APEX class and display it in a VP. Here's what I have so far:
public class stdnts {
    List<student__c> enrolledStdnts = [SELECT ID, Name, Year__c, Class__c FROM student__c LIMIT 10];
}
and
<apex:page controller="stdnts">
	<apex:form >
        <apex:pageBlock title="List of enrolled Students">
			<apex:pageBlockTable value="{! enrolledStdnts }" var="ct">
            <apex:column value="{! ct.Year__c }"/>
            <apex:column value="{! ct.Class__c }"/>
			</apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
What am I doing wrong?

 
Best Answer chosen by sayko
Akshay_DhimanAkshay_Dhiman
Hi,

I have gone through your code and found that you have not used getter and setter methods for your list.
Without getter and setter method you will not be able to reference your variables in Visualforce page even though 
your variables are public or global.

Here is the code below for your Controller class:
 
public class students 
{
   List<student__c> enrolledStdnts{get;set;} 
   public stdnts()
   {
    enrolledStdnts = [SELECT ID, Name, Year__c, Class__c FROM student__c LIMIT 10]; 
   }    
 
}



For the VF page, you don't have to perform any changes.

Hope this explanation is quite understandable as per your query.
Mark, it as best answer if you find it helpful.

Thank you.
Akshay

All Answers

Alain CabonAlain Cabon
Hi,

public List<student__c> enrolledStdnts

private: This is the default, and means that the method or variable is accessible only within the Apex class in which it is defined. If you do not specify an access modifier, the method or variable is private.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_access_modifiers.htm
 
mritzimritzi
Following is the apex code that will solve your probem:
public class stdnts {
    public List<student__c> enrolledStdnts {get{
        return new List<student__c>([SELECT ID, Name, Year__c, Class__c FROM student__c LIMIT 10]);
    }
    set;}
}

Visualforce page remains same.

Any variable that needs to be used in VF page, needs to 'public' and must have get & set method (optional) defined.

Please mark this as Best Answer, if this solves your problem.​
Akshay_DhimanAkshay_Dhiman
Hi,

I have gone through your code and found that you have not used getter and setter methods for your list.
Without getter and setter method you will not be able to reference your variables in Visualforce page even though 
your variables are public or global.

Here is the code below for your Controller class:
 
public class students 
{
   List<student__c> enrolledStdnts{get;set;} 
   public stdnts()
   {
    enrolledStdnts = [SELECT ID, Name, Year__c, Class__c FROM student__c LIMIT 10]; 
   }    
 
}



For the VF page, you don't have to perform any changes.

Hope this explanation is quite understandable as per your query.
Mark, it as best answer if you find it helpful.

Thank you.
Akshay
This was selected as the best answer