+ Start a Discussion
Sonali GonnadeSonali Gonnade 

How to use formula fields in Visual force data table.



I have created 2 cross object formula fields to get information from Item object. Now I want to show these fields on visual force page using data table. As the fields are in read only mode I am unable to display them on visual force page using onchage event. kindly suggest the further steps.


Thanks in advance.




You should be able to output formula fields in the same way as any other field using the apex:outputField component.  I'm not sure how onchange fits in to this - can you post your code and a little more information about what you are trying to achieve?

Sonali GonnadeSonali Gonnade

Thanks for your quick help.

My code is :

    <apex:pageblocktable value="{!admins}" var="q"  id="theTable" styleClass="tableClass" >
                 <apex:column headerValue="ITEMS" >
                    <apex:selectList value="{!q.IPItemID__c}" size="1" id="two">
                      <apex:selectOptions value="{!Item}">
                      <apex:actionSupport event="onchange"  action="{!test}" />
        <apex:column headerValue="DESCRIPTION" >
                    <apex:inputText value="{!q.IDescription__c}" id="desc" />
        <apex:column headerValue="UNIT PRICE" rendered="true">
                    <apex:inputText value="{!q.IPrice__c}" id="price" /> 


and onchange event for selected Item id item description and price should display which are formula fields.





Formula field is read only field so you can show the fields in visual force page by datatable in <apex:column>. Go through the sample code given below :


VF code:

                <apex:pageBlockTable value="{!propAllContacts}" var="c">

                <apex:column value="{!c.name}"/>

                <apex:column value="{!c.email}"/>

                <apex:column value="{!c.title}"/>

                <apex:column value="{!c.TestCreatedDate__c}"/>


                <apex:actionFunction action="{!GetAllContacts}" reRender="opnlCont">

                <apex:param assignTo="{!propcontactid}" value="" />




Controller code:

            List<Contact> con;

            public List<Contact> propAllContacts    { get { return con; } set { con = value; } }//End Prop propAllContacts

            public string propcontactid { get; set; }                                      

                                                public void GetAllContacts()


                                                                if(propcontactid != null && propcontactid != '')


                                                                                if(con != null && con.size() > 0)



                                                                                con = [select id,name,email,title,TestCreatedDate__c,Department,Phone from Contact WHERE AccountId =: propcontactid];


                                                }//End GetAllContacts


            TestCreatedDate__c is readonly field and you can show thsis field  in VF page.