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
HarryYHarryY 

Need to populate fields on vf page whenever new fields are added to the object

Below is the example scenario from an interview. Could anyone help here
I have a scenario where I am displaying list of fields from an Account object using standard list controller. Now I have to display all fields in the account object using a vf page. If any new fields are created in the object or any existing fields are deleted from object the vf page should dynamically get fields and their respective values. The controller should not use any fieldsets or custom settings. The code should be written in a way where there should be no need to modify either code or cofigurations when there are changes happening in the object.
Can some one please explain how to acheive this.
KrishnaAvvaKrishnaAvva
Hi Harry,

This has been solved here. Please follow the post : https://developer.salesforce.com/forums/?id=906F000000090fnIAA.

You have to use the methods of describeSObject(). More info here : https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_describesobject.htm.

Please mark this as SOLVED, if it had helped you. Thanks.

Regards,
Krishna Avva
Maharajan CMaharajan C
Hi Harry,

Yes you can do this by using the below things:

1. Custom Controller.
2. describeSObject() to fetch all the Objects 
3. Dynamic soql to query the records.
4. <apex:repeat> to iterate the Fields.

Please Find the below Example:

VF Page:

<apex:page Controller="exportContactRecords" ContentType="application/vnd.ms-excel#SalesForceExport.xls" >
  <apex:pageBlock >
      <apex:pageBlockTable value="{!contactList}" var="c">
        <apex:repeat value="{!lstFields}" var="FieldLable">  
            <apex:column value="{!c[FieldLable]}"/>  
        </apex:repeat>                   
     </apex:pageBlockTable>
  </apex:pageBlock> 
</apex:page>


Apex Class :

public class exportContactRecords
{
    public List <Contact> contactList { get; set; } 
    public List <String> lstFields { get; set; } 
    
    String query;   
    String allFields = '';
    public exportContactRecords()
    {
        String objectName = 'Contact';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(objectName ).getDescribe().fields.getMap();
        set<string> mapset = fieldMap.keyset(); 
        contactList = NEW List <Contact> (); 
        lstFields = new List<String>();
        for ( String str:mapset )
        {

            allFields += str +', '; 
            lstFields.add(str);

        }
        allFields = allFields.removeEnd(', ');

        query = 'SELECT '+ allFields + ' FROM '  + objectName;

        System.debug('----------'+query);   

        contactList = Database.query(query);
        System.debug('----------'+contactList);
    }
}


Thanks,
Maharajan.C