0

Trying to find out how to work with actionFuntion in case of usage html instead of VF. My simple piece of code is listed below.

Controller:

public class Test {
    public String variable1 {get; set;}
    public String variable2 {get; set;}

    public void mTest(){
        System.debug(variable1 + ' ' + variable2);
    }
}

Apex page:

<apex:page controller="Test">
<apex:form>
    <apex:actionFunction name="JS" action="{!mTest}">
        <apex:param assignTo="{!variable1}" name="var1" value=""/>
        <apex:param assignTo="{!variable2}" name="var2" value=""/>
    </apex:actionFunction>
</apex:form>

<form>
    <input type="text" id="input1"/><br/>
    <input type="text" id="input2"/><br/>
    <button type="button" onclick="doJS()">
        Click
    </button>
</form>

<script>
    function doJS(){
        let x = document.getElementById('input1').value;
        let y = document.getElementById('input2').value;

        JS(x, y);
    }
</script>

First to say, it doesn't work: mTest is invoked but with no parameters passed. Also, I have some questions.

  1. Usage of apex:param. It declares an order of passed arguments or what?
  2. How to send variable's from apex to the Vf?

    Thank you!

2

You should not use multiple forms in a page.

So, place your input fields inside apex:form.

Secondly, to perform actionFunction properly, use reRender attribute.

Thirdly, document.getElementById should take full DOM path.

<apex:page controller="Test">

<apex:form id="form1">
    <apex:actionFunction name="JS" action="{!mTest}" reRender="form1">
        <apex:param assignTo="{!variable1}" name="var1" value=""/>
        <apex:param assignTo="{!variable2}" name="var2" value=""/>
    </apex:actionFunction>



    <input type="text" id="input1"/><br/>
    <input type="text" id="input2"/><br/>
    <button type="button" onclick="doJS()">
        Click
    </button>
</apex:form>

<script>
    function doJS(){
        let x = document.getElementById('input1').value;
        let y = document.getElementById('input2').value;

        JS(x, y);
    }
</script>
</apex:page>
1

Following your questions:

  1. Multiple apex:param declared in an apex:actionFunction shall be interpreted in the order declared. Your example matches that.
  2. Try to put both your apex:actionFunction and inputs in the same form, that should do the trick.

Depending on what you want to do in your page, you might want to consider action functions vs remote actions (http://biswajeetsamal.com/blog/difference-between-remote-action-and-action-function-in-salesforce/).

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.