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
Yogesh LolgeYogesh Lolge 

How to fetch cutom objects data to VF page?

Hi, I want to fetch data from 4 custom objects to one VF page, how it can be achieved using one controller class. if any example is there it would be very helpful.
AbhishekAbhishek (Salesforce Developers) 
Your query is answered below,

http://burnignorance.com/salesforce-tips/how-to-fetch-and-display-data-from-a-custom-object-on-visualforce-page-in-salesforce/

It even has an example with the code snippet.

For further reference, you can try the below,

Below is the input field on the VF page

<input id="textpanel" type="text" name="txt" size="35" value="{!myoutput}"/>

 apex class Code

Public static String myoutput                                                              {get;set;}

List<custom object name> obj =select query of the field from the custom object goes here
for(custom object name obj : obj){

if(obj.fieldname!=null){
myoutput=obj.fieldname
}

}


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
Malika Pathak 9Malika Pathak 9

Hi Yogesh,

please find the solution

public without sharing class PkbContentController {
      public PKB_Content__c pos {get;set;}
      public PkbContentController(ApexPages.StandardController sc) {
          // Add all the other fields you need here instead of ...
          pos = [SELECT Home_Forum__c, ... FROM PKB_Content__c limit 1];
      }
}


This pattern "mixes in" the controller extension with the standard controller so your page can reference both; in your example you are only referencing the controller extension field:

<apex:page showHeader="false"
        standardController="KnowledgeArticle"
        extensions="PkbContentController">
    <apex:outputText value="{!pos.Home_Forum__c}"/>
    <!-- Output more fields here -->
</apex:page>


See e.g. the Building a Controller Extension (https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/) documentation for more information.

 

If you find this helpful then mark it as the best answer.