1

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;
}
1

3 Answers 3

5

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).

6
  • Maybe I didn't do this right, but I got "may not be used within an iterable component. You can use to define parameters for the function and pass iteration-specific values via the parameters". I put my controller method as the "action", and in the params, assigned my variable the value from the page... Mar 26, 2019 at 22:35
  • Look at the documentation. They suggest a workaround for this via JavaScript indirection. Call a function in your JavaScript that then calls the generated action function.
    – Phil W
    Mar 26, 2019 at 23:24
  • My current code seems to mirror the one in the doc. as far as I can tell, yet my variable is still coming out as null Apr 2, 2019 at 18:04
  • You could try looking at the generated JavaScript via the Chrome "inspect element" debugger, set a breakpoint in your "indirection" function and see if it is invoking the generated action function with the desired parameter value(s). You could also add some System.debug to an explicitly specified setter method on the property of the controller for the parameter you are passing to see what logging you get through the Developer Console.
    – Phil W
    Apr 3, 2019 at 6:46
  • It might be good to add a section to the end of your question showing what you have tried with action functions.
    – Phil W
    Apr 3, 2019 at 6:48
1

You are getting value as null in the controller because you are not using rerender attribute of apex:commandButton. If you miss this attribute then form will get submitted but value will not get assigned.

In the below code, rerendering the form element. You could choose proper element name for rerendering.

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

Controller

public String selectedRec {get;set;}

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

For more information, refer apex:actionFunction

0

You may want to refer to my answer( ActionFunction not working properly.Values not getting passed to controller ) on a similar post. Its just a Salesforce issue which is not documented anywhere since I last checked. It's something which creates so many issues when trying to pass data to apex without using rerender in apex:actionfunction

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .