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
DannyK89DannyK89 

Converting apex:columns to apex:outputFields

I want to make my visual force page have outputFields so that it is easier to read. Here is what I have so far. insurance_insured has a look up relationship with Account.

 

 

<apex:page standardController="Account">
 <apex:form >
    <apex:pageBlock title="General Liability">
        <apex:pageBlockTable value="{!account.insurance_insured__r}" var="item">
            <apex:detail relatedList="false"/>
            <apex:column title="Insurance" value="{!item.name}" rendered="{!item.Insurance_Type__c == 'General Liability'}"/>
            <apex:column title="Action" rendered="{!item.Insurance_Type__c == 'General Liability'}">
                <apex:commandButton value="About" onclick="window.location='/{!item.id}'; return false;"/>
            </apex:column>
            <apex:column title="Insurance Carrier" value="{! item.Insurance_Carrier__c}" rendered="{!item.Insurance_Type__c == 'General Liability'}"/>
            <apex:column title="Effective Date" value="{! item.Effective_Date__c}" rendered="{!item.Insurance_Type__c == 'General Liability'}"/>
            <apex:column title="Expiration Date" value="{! item.Expiration_Date__c}" rendered="{!item.Insurance_Type__c == 'General Liability'}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Professional Liability">
        <apex:pageBlockTable value="{! account.insurance_insured__r}" var="item">
            <apex:column title="Insurance" value="{! item.name}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>
             <apex:column title="Action" rendered="{!item.Insurance_Type__c == 'Professional Liability'}">
                <apex:commandButton value="About" onclick="window.location='/{!item.id}'; return false;"/>
            </apex:column>
            <apex:column title="Insurance Carrier" value="{! item.Insurance_Carrier__c}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>
            <apex:column title="Effective Date" value="{! item.Effective_Date__c}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>
            <apex:column title="Expiration Date" value="{! item.Expiration_Date__c}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Work Comp Liability">
        <apex:pageBlockTable value="{! account.insurance_insured__r}" var="item">
            <apex:column title="Insurance" value="{! item.name}" rendered="{!item.Insurance_Type__c == 'Work Comp Liability'}"/>
             <apex:column title="Action" rendered="{!item.Insurance_Type__c == 'Work Comp Liability'}">
                <apex:commandButton value="About" onclick="window.location='/{!item.id}'; return false;"/>
            </apex:column>
            <apex:column title="Insurance Carrier" value="{! item.Insurance_Carrier__c}" rendered="{!item.Insurance_Type__c == 'Work Comp Liability'}"/>
            <apex:column title="Effective Date" value="{! item.Effective_Date__c}" rendered="{!item.Insurance_Type__c == 'Work Comp Liability'}"/>
            <apex:column title="Expiration Date" value="{! item.Expiration_Date__c}" rendered="{!item.Insurance_Type__c == 'Work Comp Liability'}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
    <apex:pageBlock title="Other Liability">
        <apex:pageBlockTable value="{! account.insurance_insured__r}" var="item">
            <apex:column title="Insurance" value="{! item.name}" rendered="{!item.Insurance_Type__c == 'Other Liability'}"/>
             <apex:column title="Action" rendered="{!item.Insurance_Type__c == 'Other Liability'}">
                <apex:commandButton value="About" onclick="window.location='/{!item.id}'; return false;"/>
            </apex:column>
            <apex:column title="Insurance Carrier" value="{! item.Insurance_Carrier__c}" rendered="{!item.Insurance_Type__c == 'Other Liability'}"/>
            <apex:column title="Effective Date" value="{! item.Effective_Date__c}" rendered="{!item.Insurance_Type__c == 'Other Liability'}"/>
            <apex:column title="Expiration Date" value="{! item.Expiration_Date__c}" rendered="{!item.Insurance_Type__c == 'Other Liability'}"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
   </apex:form>
</apex:page>

 

 

Best Answer chosen by Admin (Salesforce Developers) 
larkinrichardslarkinrichards

Hi Danny,

 

How about this - replace the pageBlockTable with a repeat element.  Inside the repeat element, have a pageBlockSection that only renders if the insurance type matches what you want.  Inside this pageBlockSection, have the output fields you need.   

 

Example:

 

<apex:pageBlock title="General Liability">
    <apex:repeat value="{!account.insurance_insured__r}" var="item">
        <apex:pageBlockSection columns="2" 
			rendered="{!item.Insurance_Type__c == 'General Liability'}">
			<apex:outputField value="{!item.name}" />
			<apex:pageBlockSectionItem >
				<apex:commandButton value="About" 
					onclick="window.location='/{!item.id}'; return false;"/>
			</apex:pageBlockSectionItem>
			<apex:outputField value="{! item.Insurance_Carrier__c}" />
			<apex:outputField value="{! item.Effective_Date__c}" />
			<apex:outputField value="{! item.Expiration_Date__c}" />
		</apex:pageBlockSection>
	</apex:repeat>
</apex:pageBlock>

EDIT: fixed wrong closing tag.

 

All Answers

Ritesh AswaneyRitesh Aswaney
I'm not quite sure if I've misunderstood your question, but is it just a case of 

<apex:column title="Insurance" value="{!item.name}" rendered="{!item.Insurance_Type__c == 'General Liability'}"/>

should be
<apex:outputField value="{!item.name}" rendered="{item.Insurance_Type__c == 'General Liability'}" />

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_outputField.htm?SearchType=Stem&Highlight=outputField
DannyK89DannyK89

The problem is that doesn't work. When I change it to a output field it does not render. 

larkinrichardslarkinrichards

Hi Danny,

 

It seems like you are using a apex:pageBlockTable to loop over a detail component (which I don't think will render) and a set of columns.  Is your intention to have a repeating pageBlock for each item?

 

If you don't want the fields rendered as columns, then consider using a apex:repeat tag to repeat a group of output fields.

 

Alternatively, if you want to keep the data formatted as a table, you could enclose an apex:outputField in a apex:column tag, but I don't think that is what you are looking for.

sravusravu

Try to change you <apex:column> to the code hilighted and see if it works:

 

<apex:column title="Effective Date" value="{! item.Effective_Date__c}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>

<apex:column>
<apex:outputField value ="{!item.Effective_date__c}" rendered="{!item.Insurance_Type__c == 'Professional Liability'}"/>
</apex:column>
jwetzlerjwetzler

I think you need to actually describe what your problem is.

 

If your apex:column's value attribute refers to an sObject field and it's inside a pageBlockTable, it's automatically converted to an outputField behind the scenes.

 

But I'm not sure where you're going with having the rendered attribute on a column referring to just item in the iteration.  Do you want an entire column hidden in a specific case, or do you just want certain cells to be hidden under specific conditions?  Sravu's example would conditionally display individual cells in your table.  The way you have it now I think it will show or hide columns based on the very first entry in your list, which I can't imagine is what you want.

AhmedPotAhmedPot

I think using apex:outputfield inside apex:column tags will work abosulutely fine as mentioned in above post.

DannyK89DannyK89

I tried the apex:outputField inside of the apex:column tags and I get a bunch or blank lines my column. What I want to do is make outputFields for the values inside the apex:column fields. The object that is used in the apex:columns is a related list in account. I want to show field from the object that is a related list int he account object on an visualforce page as ouputFields. I am having trouble because everytime I try to use "account.insurance_insured__r" I get an error. I want to know if there is a way to access the fields from a related list object on a visualforce page as an apex:outputField.  

Ritesh AswaneyRitesh Aswaney

Hey, did you try what larkinrichards suggested in his post above - i.e. use the <apex:repeat> tag to iterate over the collection, rather than <apex:pageBlockTable> and use <apex:outputField> inside it.

 

i.e.

<apex:repeat var=......

<apex:outputField value="{!......

 

</apex:repeat>

DannyK89DannyK89

I tried the apex:repeat tag as well. It still looks like a column but know everything is squished together. I am really trying to make the values look like fields and not like columns. 

sravusravu

Instead of  "account.insurance_insured__r" try using

 

account.insurance_insured__r[0].fieldname

DannyK89DannyK89

Where would I put that? In the apex:repeat ?

larkinrichardslarkinrichards

Hi Danny,

 

How about this - replace the pageBlockTable with a repeat element.  Inside the repeat element, have a pageBlockSection that only renders if the insurance type matches what you want.  Inside this pageBlockSection, have the output fields you need.   

 

Example:

 

<apex:pageBlock title="General Liability">
    <apex:repeat value="{!account.insurance_insured__r}" var="item">
        <apex:pageBlockSection columns="2" 
			rendered="{!item.Insurance_Type__c == 'General Liability'}">
			<apex:outputField value="{!item.name}" />
			<apex:pageBlockSectionItem >
				<apex:commandButton value="About" 
					onclick="window.location='/{!item.id}'; return false;"/>
			</apex:pageBlockSectionItem>
			<apex:outputField value="{! item.Insurance_Carrier__c}" />
			<apex:outputField value="{! item.Effective_Date__c}" />
			<apex:outputField value="{! item.Expiration_Date__c}" />
		</apex:pageBlockSection>
	</apex:repeat>
</apex:pageBlock>

EDIT: fixed wrong closing tag.

 

This was selected as the best answer