+ Start a Discussion
studentstudent 

Apex method in visualforce page

Hi!

 

I want to include an apex method, which needs an object as variable, into my own visualforce page. So, if anybody clicks on a commandLink the method should be envoked with the relative object.

 

My approach:

 

<apex:pageBlock title="Title">
<apex:dataTable value="{!method}" var="object" width="100%">
<apex:column >
<apex:facet name="header">ID</apex:facet>
<apex:form >
<apex:commandLink value="{!object.name}" action="{!method(object)}"/>
</apex:form>
</apex:column>
</apex:dataTable>
</apex:pageBlock>

 

 The method [in dataTable value] is defined in an apex class and returns a list of several objects with some attributes, e.g. the name.

The method(object) [in commandLink action] is also defined in an apex class. Unfortunately I can't refer to the object.

 

Does anybody know why?

 

Thanks in advance!

 

Phil 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Commandlink action methods can't take parameters in this way. You'll need something along these lines:

 

 

<apex:commandLink action="{!editConsumable}" value="{!object.name}" styleClass="btn" status="status">

<apex:param name="theObject" assignTo="{!selectedObject}" value="{!object}"/>

</apex:commandLink>

 

You'll need to change your action method to take no parameters, add a property to your controller called selectedObject, and then in the action method you'll reference it to determine the object associated with the command.

 

Message Edited by bob_buzzard on 11-19-2009 10:13 AM

All Answers

bob_buzzardbob_buzzard

Commandlink action methods can't take parameters in this way. You'll need something along these lines:

 

 

<apex:commandLink action="{!editConsumable}" value="{!object.name}" styleClass="btn" status="status">

<apex:param name="theObject" assignTo="{!selectedObject}" value="{!object}"/>

</apex:commandLink>

 

You'll need to change your action method to take no parameters, add a property to your controller called selectedObject, and then in the action method you'll reference it to determine the object associated with the command.

 

Message Edited by bob_buzzard on 11-19-2009 10:13 AM
This was selected as the best answer
studentstudent

unfortunately param value can't contain objects...but after a little time, everything works :-)

So thanks a lot for your great answer!

bob_buzzardbob_buzzard
Ah yes - I think the param needs to be something that can appear in the URL that the page is submitted to.