1

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.

2

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

1

It is a known issue in salesforce. You can refer this blog for more detail.

You can use command link instead of button in this case. Please follow this post for solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.