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
pooja biswaspooja biswas 

get custom fields

Hi
I am understading the basics of metadata of fetching account info.
below is the code 
public class selectAllSOQLExampleController
{
    String SobjectApiName = 'Account';
   
    List<Account> accList=new List<Account>();
   
    public String query{get;set;}

    public List<Account> getAccList() 
    {
       Map<String,Schema.SObjectType> schemaMap=Schema.getGlobalDescribe();
          
       Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
     
       String commaSeparatedFields = '';
     
       for(String fieldName : fieldMap.keyset())
       {
            if(commaSeparatedFields == null || commaSeparatedFields == '')
            {
                commaSeparatedFields = fieldName;
            }
            else
            {
                commaSeparatedFields = commaSeparatedFields + ', ' + fieldName;
            }
       }
 
       query = 'select ' + commaSeparatedFields + ' from ' + SobjectApiName + ' Limit 10 ';
   
       accList = Database.query(query);
     
       return accList;
 
    }
}

<apex:page controller="selectAllSOQLExampleController">
  
   <apex:form>
        <apex:pageBlock>
                       
            <apex:pageBlockSection title="Account table" 
                                                     columns="1" 
                                                     collapsible="false">
                                   
                <apex:pageBlockTable value="{!accList}" 
                                                      var="acc">

                    <apex:column value="{!acc.name}"/>
                    <apex:column value="{!acc.phone}"/>
                    <apex:column value="{!acc.rating}"/>
                    <apex:column value="{!acc.industry}"/>
                    <apex:column value="{!acc.accountnumber}"/>
                    
                </apex:pageBlockTable>
 
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>
My requirement is I want to display custom fields also.
Pls help me in tweeking my code.


pooja
Best Answer chosen by pooja biswas
CloudGeekCloudGeek
Hello Pooja,

You just need to include columns for custom fields too.
Like shown in below code :
 
<apex:page controller="selectAllSOQLExampleController">
  
   <apex:form >
        <apex:pageBlock >
                       
            <apex:pageBlockSection title="Account table" 
                                                     columns="1" 
                                                     collapsible="false">
                                   
                <apex:pageBlockTable value="{!accList}" 
                                                      var="acc">

                    <apex:column value="{!acc.name}"/>
                    <apex:column value="{!acc.phone}"/>
                    <apex:column value="{!acc.rating}"/>
                    <apex:column value="{!acc.industry}"/>
                    <apex:column value="{!acc.accountnumber}"/>
                    <apex:column value="{!acc.Account_Status__c}"/>
                    <apex:column value="{!acc.Active__c}"/>
                </apex:pageBlockTable>
 
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Hope that helps!

Mark this as solution if this helps in resolving issue.

All Answers

CloudGeekCloudGeek
Hello Pooja,

You just need to include columns for custom fields too.
Like shown in below code :
 
<apex:page controller="selectAllSOQLExampleController">
  
   <apex:form >
        <apex:pageBlock >
                       
            <apex:pageBlockSection title="Account table" 
                                                     columns="1" 
                                                     collapsible="false">
                                   
                <apex:pageBlockTable value="{!accList}" 
                                                      var="acc">

                    <apex:column value="{!acc.name}"/>
                    <apex:column value="{!acc.phone}"/>
                    <apex:column value="{!acc.rating}"/>
                    <apex:column value="{!acc.industry}"/>
                    <apex:column value="{!acc.accountnumber}"/>
                    <apex:column value="{!acc.Account_Status__c}"/>
                    <apex:column value="{!acc.Active__c}"/>
                </apex:pageBlockTable>
 
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Hope that helps!

Mark this as solution if this helps in resolving issue.
This was selected as the best answer
piyush parmarpiyush parmar
Hi Pooja,

You did correct in apex controller. You are calling fields custome/standar in your query. But when you are show the records on the Visualforce page, You have added few standard fields in apexpageblocktable as column. Please add custome fields in
eg:
<apex:column value="{!acc.Custome_Fields__c}" />
CloudGeekCloudGeek
You will get to see output like (Last 2 - Columns are CUSTOM FIELDS):

User-added image
Lalit Karamchandani 27Lalit Karamchandani 27


Hi pooja biswas

public class DynamicAccountFieldsLister {

    public DynamicAccountFieldsLister(ApexPages.StandardController controller) {
        controller.addFields(editableFields);
    }

    public List<String> editableFields {
        get {
            if (editableFields == null) {
                editableFields = new List<String>();
                editableFields.add('Industry');
                editableFields.add('AnnualRevenue');
                editableFields.add('NumberofLocations__c'); // here you have to specify custome fields
            }
            return editableFields ;
        }
        private set;
    }
}

<apex:page standardController="Account" extensions="DynamicAccountFieldsLister">
    <apex:pageMessages /><br/>
    <apex:form>
        <apex:pageBlock title="Edit Account" mode="edit">
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!Account.Name}" />
                <apex:repeat value="{!editableFields}" var="f">
                    <apex:inputField value="{!Account[f]}" /> </apex:repeat>
            </apex:pageBlockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>