[SalesForce] commandLink and commandButton with image not passing parameters

I'm trying to pass a parameter to a method in a visualforce page when the users clicks on a detail icon (a static resource image).

I'm trying with a commandButton since I need the button to be an image. The problem is that the parameter doesn't seem to get passed, cause from this code I get the error message 'Nothing found'. Do you have any idea why this happen? Should I use another approach?

page code:

<apex:commandButton action="{!getRichiestaDetail}" image="{!$Resource.detail_icon}" id="getRichiestaDetail" style="border:none;background:none;padding:0px;vertical-align:top;">
                            <apex:param name="richiestaId" value="{!richiesta.Id}" assignTo="{!detailRichiestaId}"/>
                    </apex:commandButton> 

controller code:

public PageReference getRichiestaDetail() {
    List<Riga_Richiesta__c> righeRichiesta = 
                    [SELECT Id, Richiesta__c 
                    FROM Riga_Richiesta__c 
                    WHERE Richiesta__c =: detailRichiestaId];
    if (righeRichiesta != null && righeRichiesta.size() > 0) {
        showRichiestaDetail = true;
        righeRichiestaDetail = righeRichiesta;
    } else {
        ApexPages.Message errorMsg = new ApexPages.Message(ApexPages.severity.ERROR, 'Nothing found');
        ApexPages.addMessage(errorMsg); 
    }
    return null;
}

Also, is it bad that I make a query call everytime someone clicks on a detail icon?

Thanks.

Best Answer

As Pramod says, this is a known issue, the alternative workaround is to add reRender=... to your commandButton

<apex:commandButton action="{!getRichiestaDetail}" image="{!$Resource.detail_icon}" 
                    id="getRichiestaDetail" rerender="somecomponent"
                    style="border:none;background:none;padding:0px;vertical-align:top;">
    <apex:param name="richiestaId" value="{!richiesta.Id}" 
                assignTo="{!detailRichiestaId}"/>
</apex:commandButton> 

This is covered here: http://blog.jeffdouglas.com/2010/03/04/passing-parameters-with-a-commandbutton/ and here: http://bobbuzzard.blogspot.com/2011/07/passing-parameters-to-apex-method-from.html

As for your second question - making a query for each detail click - is not per se bad practice. It guarantees that the latest database state is retrieved and is fundamentally the same behavior that SFDC itself uses when you click on the View link on any related list item. You could of course optimaize this by having the controller prefetch all possible details for the presented page - but then you might run into other limits like Heap size or possibly ViewState. This second question, if you still have issues should be posted as a new question in SFSE