You need to sign in to do that
Don't have an account?
sayko
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?
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:
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
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
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.
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:
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