+ Start a Discussion
Ketan Solanki29Ketan Solanki29 

How to Display dynamic field of Object in Visualforce page

I have written a Controller class

 

public with sharing class SampleTxController {
public List<Transaction__c> trxList{get;set;}

    public SampleTxController(){
        trxList = [ SELECT Id,Name,Amount__c,Balance__c,Insert_Date__c,Business_Unit__c FROM Transaction__c LIMIT 999 ];
    }
}

 

also have Visual force page to display transaction records 


<apex:pageblock id="PBId" >
<table >
    <apex:repeat value="{!trxList}" var="tx">
        <tr>
             <td> <apex:outputlabel value="{!tx.Name}" /> </td>
             <td> <apex:outputlabel value="{!tx.Amount__c}" /> </td>
       </tr>
</apex:repeat>
</table>
</apex:pageblock>

 

My Question is that i want to display each and every field dynamically for each transaction then what i should do ?

 

for each field i am using <apex:outputlabel value="{!tx.fieldName}" /> which feasible way to include each and every field with outputlabel.

if i have selected 100 of fields then i have to add all 100 fields in outputlabel.

 

please help me if anyone have solution to solve this.

 

 

Navatar_DbSupNavatar_DbSup

Hi,

 

Try the below code snippet as reference:

 

----------- Vf page------------

 

<apex:page controller="displyafielddyanmaicallybyquery" >

<Apex:form >

  <apex:repeat value="{!fieldapiname}" var="cc">

 <Apex:outputLabel >Field name <b>{!cc}</b></Apex:outputLabel> <apex:outputLabel value="{!con[cc]}"></apex:outputLabel> <br/>

  </apex:repeat>

  </Apex:form>

</apex:page>

 

------------- Apex controller----------------

public class displyafielddyanmaicallybyquery

{

public list<string>fieldapiname{get;set;}

public contact con{get;set;}

 public Set<string> flds;

public displyafielddyanmaicallybyquery ()

{

    flds=new set<string>();

    fieldapiname=new list<string>();

    con=[Select c.txtdate__c, c.test_textarea__c,Jigsaw,Text_Area_Rich__c ,JigsawContactId,c.test_Text_Area_Long__c, c.number__c, c.muliselectpicklist__c, c.chkdate__c, c.chekbox__c, c.Units__c, c.Title, c.SystemModstamp, c.Salutation, c.Role__c, c.ReportsToId, c.Phone, c.OwnerId, c.OtherStreet, c.OtherState, c.OtherPostalCode, c.OtherPhone, c.OtherCountry, c.OtherCity, c.Name, c.MobilePhone, c.MasterRecordId, c.MailingStreet, c.MailingState, c.MailingPostalCode, c.MailingCountry, c.MailingCity, c.Level__c, c.LeadSource, c.LastName, c.LastModifiedDate, c.LastModifiedById, c.LastCUUpdateDate, c.LastCURequestDate, c.LastActivityDate, c.Languages__c, c.IsDeleted, c.Id, c.HomePhone, c.FirstName, c.Fax, c.EmailBouncedReason, c.EmailBouncedDate, c.Email, c.Description, c.Department, c.CreatedDate, c.CreatedById, c.ConnectionSentId, c.ConnectionReceivedId, c.Broker__c, c.Birthdate, c.AssistantPhone, c.AssistantName, c.AccountId From Contact c where id='0039000000C37mx'];

   Map<string,Schema.SObjectField> mpConField=Contact.getSObjectType().getDescribe().fields.getMap();

   flds=mpConField.keySet();

   for(String name : flds)

        {

            Schema.DescribeFieldResult fldResult=mpConField.get(name).getDescribe();

            if(string.valueof(fldResult.getName())!='Jigsaw' || string.valueof(fldResult.getName())!='JigsawContactId' )

            fieldapiname.add(string.valueof(fldResult.getName()));

        }

}

}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 

Ketan Solanki29Ketan Solanki29

Thanks, Ankit


The bellow code given by you is work fine.


<apex:outputLabel value="{!con[cc]}"></apex:outputLabel>

 

But, problem with date and datetime fields. i want to display date in some format than what i have to write here in apex page.