0

I have a visual force page. I have two values on this page that I need to pass to its controller. I am able to put these values in hidden fields on the page. I am now trying to use an apex:actionFunction to send them over to the controller.

Here is my relevant section of the VF Page.

<apex:actionFunction name="passStringToController" immediate="true" action="{!saveCaseRecord}" rerender="">
                        <apex:param name="p1" value="{!Case.Type}" assignTo="{!CaseType}" />
                        <apex:param name="p2" value="{!Case.Case_Type_Details__c}" assignTo="{!CaseSubType}" />
                    </apex:actionFunction>

Here is the relevant section of the apex controller code. Edited: The CaseType and CaseSubType variables have been declared as public in the controller.

public string CaseType {get;set;}
public string CaseSubType {get;set;}

    public PageReference saveCaseRecord()
    {

                System.Debug('BeforE calling createCase');
                System.Debug('CaseType ' + CaseType);
               System.Debug('CaseSubType ' + CaseSubType);
    }

I am definitely missing a piece here, because I am getting a null each time in the controller. I wrote a javascript function, but I don't believe it is firing. I am never seeing this value in the console log.

function passStringToController()
      {
                console.log('Function to get controller - passStringToController');
       };

I am calling the javascript passStringToController() from another javascript method on the same page.

4
  • 1
    Can you show the caseType and caseSubType declaration? you do not need js / actionfuncton to pass the value controller
    – Rao
    Aug 16, 2017 at 20:15
  • 1
    how you are calling this actionFunction? Aug 16, 2017 at 20:23
  • Don't create a JS function wit the same name as the actionFunction
    – Eric
    Aug 16, 2017 at 21:39
  • What is the URL you are using to load the page and are you using a standardcontroller? Better yet post all your code.
    – Eric
    Aug 16, 2017 at 21:53

3 Answers 3

1

When you call your passStringToController method you need to set the parameters there.

function passStringToController1()
      {
                passStringToController('Param 1', 'Param 2');
                console.log('Function to get controller - passStringToController');
       };

Then, in your action function

<apex:actionFunction name="passStringToController" immediate="true" action="{!saveCaseRecord}" rerender="">
      <apex:param name="p1" value="" assignTo="{!CaseType}" />
      <apex:param name="p2" value="" assignTo="{!CaseSubType}" />
</apex:actionFunction>

I am not entirely sure why setting the value directly would not work, as it should, but when I do it, that's usually the format I use.

1
  • You do not need to pass them if the param has the value set
    – Eric
    Aug 16, 2017 at 21:51
0

Not sure about your actionfunction approach, but the way to get the param value in controller is As a poc try this Page:
Controller:

public string CaseType{get;set;}
public PageReference saveCaseRecord()
    {
    System.Debug('CaseType ' + CaseType);
}
2
  • if you use assignTo then apexpages.currentpage().getparameters().get() not needed, it will map to setter property Aug 16, 2017 at 20:28
  • corrected @SantanuBoral I was copy pasting from another example and messed up
    – Rao
    Aug 16, 2017 at 20:32
0

Following piece of your code works for me at my DE.

I think main issue at your question is calling of the function.

Visualforce

<apex:page standardController="Case" extensions="ActionFunctionController">
<script>
    window.onload = function() {
      passStringToController();
    };
</script>

<apex:form >
    <apex:actionFunction name="passStringToController" immediate="true" action="{!saveCaseRecord}" rerender="values">
        <apex:param name="p1" value="{!Case.Type}" assignTo="{!CaseType}" />
    </apex:actionFunction>
    <apex:outputText value="{!CaseType}" label="You have selected:" id="values" />
</apex:form>
</apex:page>

Controller

public class ActionFunctionController
{
    public string CaseType {get;set;}
    public string CaseSubType {get;set;}

    public ActionFunctionController(ApexPages.StandardController con)
    {

    }

    public PageReference saveCaseRecord()
    {
        System.Debug('BeforE calling createCase');
        System.Debug('CaseType ' + CaseType);
        return null;
    }
}

You must log in to answer this question.

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