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
wsthesiswsthesis 

Passing Parameters from Visualforce Page to Controller methods

Am I allowed to pass a parameter from my visualforce page to a controller method?


I want to create a method which returns boolean TRUE or FALSE depending on if the related list contains any records. I'm trying to avoid creating 15-20 get/set methods.


 

Code:
VisualForce Page Segment:
<apex:page standardController="Account" extensions="AccountHiddenListControllerExtension">
    </apex:detail>
        <apex:relatedList list="contacts" rendered="{!doesRelatedListExist('contacts'}">>
        </apex:relatedList>
        <apex:relatedList list="ActivityHistories" rendered="{!doesRelatedListExist('ActivityHistories'}">>
        </apex:relatedList>
    </apex:detail>
</apex:page>

Controller class:
    public boolean doesRelatedListExist(String listName)
    {

        if (listName.equals('contacts'))
        {
            return ((this.getContactList().size()) > 0);
        }
        if (listName.equals('ActivityHistories'))
        {
            return ((this.getActivityHistoriesList().size()) > 0);
        }

        // default value
        return true;
    }


 

The controller saves correctly; however, i get the following error message when saving the visualforce page:
Error: Unknown function doesRelatedListExist. Check spelling. 


Thanks in advance.



Message Edited by wsthesis on 12-25-2008 06:26 PM
Best Answer chosen by Admin (Salesforce Developers) 
dchasmandchasman
This gets to be so much simpler in the Spring '09 release because I finally added support for List and array getSize() in VF formulas which would allow you to simply use rendered="{!account.contacts.size > 0}" but that will have to wait a little bit longer.

The good news is that there is a 100% VF component based way to do this - no flurry of apex code getters etc. I created a not exactly obvious custom component:

Code /apexcomponent/renderIfNotEmpty:
<apex:component>
    <apex:attribute name="value" type="SObject[]" description="TODO: Describe me"/>
    
    <apex:repeat var="x" value="{!value}" first="0" rows="1">
        <apex:componentBody/>
    </apex:repeat>    
</apex:component>

 that can be used like this:

Code:
<apex:page standardController="Account">
    <apex:detail relatedList="false"/>

    <c:renderIfNotEmpty value="{!account.contacts}">
        <apex:relatedList list="Contacts"/>
    </c:renderIfNotEmpty>

    <c:renderIfNotEmpty value="{!account.activityHistories}">
        <apex:relatedList list="ActivityHistories"/>
    </c:renderIfNotEmpty>
</apex:page>

 



Message Edited by dchasman on 12-30-2008 11:20 PM

All Answers

JeremyKraybillJeremyKraybill
AFAIK there is no way to do what you are trying to do -- you'll need 15-20 get methods. But that's not terrible, since it will be less code per list than the example code you provided :)

Jeremy Kraybill
Austin, TX
dchasmandchasman
This gets to be so much simpler in the Spring '09 release because I finally added support for List and array getSize() in VF formulas which would allow you to simply use rendered="{!account.contacts.size > 0}" but that will have to wait a little bit longer.

The good news is that there is a 100% VF component based way to do this - no flurry of apex code getters etc. I created a not exactly obvious custom component:

Code /apexcomponent/renderIfNotEmpty:
<apex:component>
    <apex:attribute name="value" type="SObject[]" description="TODO: Describe me"/>
    
    <apex:repeat var="x" value="{!value}" first="0" rows="1">
        <apex:componentBody/>
    </apex:repeat>    
</apex:component>

 that can be used like this:

Code:
<apex:page standardController="Account">
    <apex:detail relatedList="false"/>

    <c:renderIfNotEmpty value="{!account.contacts}">
        <apex:relatedList list="Contacts"/>
    </c:renderIfNotEmpty>

    <c:renderIfNotEmpty value="{!account.activityHistories}">
        <apex:relatedList list="ActivityHistories"/>
    </c:renderIfNotEmpty>
</apex:page>

 



Message Edited by dchasman on 12-30-2008 11:20 PM
This was selected as the best answer
JeremyKraybillJeremyKraybill
Very helpful post as always, Doug! I can think of a number of other useful scenarios for such a component, thanks for posting it.

Jeremy Kraybill
Austin, TX
TehNrdTehNrd

dchasman wrote:
This gets to be so much simpler in the Spring '09 release because I finally added support for List and array getSize() in VF formulas which would allow you to simply use rendered="{!account.contacts.size > 0}" but that will have to wait a little bit longer.



Nice! I will definitely be looking forward to this new feature.
miteshsuramiteshsura

I am trying to do it in pageblock, but not able to fig it out.

 

VF:

<apex:column headerValue="Remove">  
                    <apex:commandButton value="Remove" action="{!removeProduct}" />
                    <apex:param assignTo="{!removeProductCode}" value="{!s.pbEntry.ProductCode}"/>  
                </apex:column>

 

In Controller, I have String Obj removeProductCode and method removeProduct. But for some reason I am not able to get the

"s.pbEntry.ProductCode" in controller. I tried name attr in <apex:param/> as well.

 

thanks for the time..

TehNrdTehNrd

Try this:

 

 

<apex:column headerValue="Remove">  
	<apex:commandButton value="Remove" action="{!removeProduct}" rerender="tableId">
		<apex:param assignTo="{!removeProductCode}" value="{!s.pbEntry.ProductCode}" name="productCode"/>  
	</apex:commandButton>
</apex:column>

1) Added param tag inside of the commandButton tag

2) Added rerender to button. There is (maybe was) a bug where param doesn't work with commandButton if rerender is not defined.

3) Also added name attribute to param as I have seen strange behavior when this is not added.

 

Hope that helps,

Jason

 

miteshsuramiteshsura

I tried many different solution, one was what you mentioned.

for now I am using the the apex:commandlink and is working fine. . 

 

thanks

ram4SFDCram4SFDC

While trying to use  {!account.contacts.size > 0} in visualforce page i get the error Error: Invalid field 'size' for SObject 'Contact'.  I have a VF page with standardController="Account". It does not have any extensions.