[SalesForce] How to get this button to pass data to controller

UPDATE

I have the following function:

<apex:actionFunction action="{!selectRec}" name="recordSelectFunction">
                        <apex:param name="selectedID" assignto="{!selectedRec}" value=""/>
                    </apex:actionFunction>
        </apex:form>

And this, inside of an iterative block (where the iterated var record is "item".

<a style="cursor:pointer; font-size:14px" onclick="recordSelectFunction('{!item.Id}')">Select Record</a>

Based on documentation, expecting onclick to call that apex:actionFunction, which passes "item.id" as the parameter, meaning my controller var would be set to that ID. !selectRec in the controller simply prints out the variable to start, and it's null.


I would like a button to be clicked and use param to set a certain variable in the controller to a value from the VF page. I am hoping someone can either tell me why it's not possible, or (hopefully) how to correctly develop this. See bottom for link to the StackExchange question that got me started.

I have a block table that loops over a list of records from the controller, and I output things like a date field, ID, Name, etc. and the last thing I do it add the following button:

<apex:form>
 <apex:commandButton value="Select Record" action="{!selectRecord}">
                         <apex:param name="recordID" assignto="{!selectedRec}" value="{!item.ID}"/>

                         // "item" is the iterated var

  </apex:commandButton>
  </apex:form>

the goal is for that button to call a function

public pageReference selectRecord(){
    system.debug('the record ID is : ' + selectedRec);
    return null;
}

Best Answer

Take a look at the apex:actionFunction. This allows you to generate a JavaScript function and to call this function from JavaScript in your page (e.g. in an onclick event). The parameters to the function can be bound (or "assigned") to properties on the controller, and these will be updated before the associated method is called on the controller.

Please do note that you will need to indirectly invoke the action function if you need the invocation within an iteration (which you appear to need).

Related Topic