[SalesForce] How to pass parameters from a VF page to a controller extension

I'm trying to get a few inputs from the user in a VF page and passing them to the controller for processing and eventually updating the page's current record.

I've been looking all around for a solution and used every possible variation I could find on how to pass parameters from an actionfunction to my controller extension. For example:
Use param with the assignto attribute and a property in the extension (initialized in the constructor and not initialized).
Use param without assignto and method arguments in the controller extension method.
Use param and pull the values using getparameters() in two variations.

So far i know the method in the controller is being called and fails getting the correct values from the page, and in the VF page the code gets the correct values.

Please help me figure this out, I'm attaching the relevant parts of the code:

VF

<apex:page standardController="Subject__c" sidebar="false" showHeader="true" extensions="ControllerExt">
<apex:form id="form">
    <head>
        <c:BootstrapStyleSheet />
        <apex:actionFunction name="updateFields" action="{!updateCurrentRecord}">
            <apex:param name="inputTextField" value="" assignTo="{!inputText}"/>                
        </apex:actionFunction>
        <apex:actionFunction name="cancel" action="{!cancelOperation}"></apex:actionFunction>
    </head>
<script>
        function onClickScreen(){
//getting the input from the <input> elements
            var number = document.getElementById("someText").value;
            updateFields(number);
        }
        function onClickCancel(){
            cancel();
        }
    </script>

Controller Extension

public with sharing class ControllerExt {

public String inputText {get; set;}

public ControllerExt (ApexPages.StandardController stdController){
    inputText = '';
}

public PageReference updateCurrentRecord(){
//Here i use these values just for checking if it will go into the if or not (it will return to the previous page if it does go in, but it doesn't)
//checking if it was received through the property's {get; set;}
    if (inputText.equals('123')){
        return previouPage();
    }
//checking if i can get it with getParameters()
    inputText = ApexPages.currentPage().getParameters().get('inputTextField'); 
    if (inputText.equals('123')){
        return previouPage();
    }
    return null;
}

public PageReference cancelOperation(){
    return previouPage();
}

private PageReference previouPage(){
    PageReference redirect = new PageReference('/' + ApexPages.currentPage().getParameters().get('id'));
    redirect.setRedirect(true);     
    return redirect;
}
}

Any suggestion or help will be greatly appreciated!

Best Answer

Main Issue: No rerender on the action function. Removing that produces an attempt to dereference null object. SO I essentially added that to the actionFunction and it all started to work.

Issue has been there for a LONG time:

http://www.laceysnr.com/apexactionfunction-and-apexparam-or-how/

You must specify a rerender attribute in the <apex:actionFunction> tag! Without this the generated function does take any parameters, but as soon as you put it in then it does - even if you just leave it empty, Go figure.

Here is a minimal reproducible example that works:

Class

public class myExample{

    public String inputText {get; set;}

    public void updateCurrentRecord(){
        if (inputText.equals('123')){
            ApexPages.addMessage(
                    New ApexPages.Message(
                            ApexPages.Severity.INFO, 'You entered ' + inputText
                    )
            );
        }

    }

}

VF Page

<apex:page controller="myExample">

<script>
        function onClickScreen(){

            var number = document.getElementById("someText").value;
            console.log(number);
            updateFields(number);
        }
        function onClickCancel(){
            cancel();
        }
    </script>

<apex:pageMessages id="msgs"/>
<apex:form id="form">

        <apex:actionFunction name="updateFields" action="{!updateCurrentRecord}" rerender="msgs">
            <apex:param name="inputTextField" value="" assignTo="{!inputText}"/>                
        </apex:actionFunction>

        <input type="text" id="someText"/>
        <button onclick="onClickScreen();" type="button">Click Me</button>
</apex:form>

</apex:page>

Enter 123 in the input and click the button, boom.

Related Topic