0

I am using javaScript in Visualforce page to set the value of String variable. How can I pass this updated value to the controler?

  function saveFunctionality()
        { document.getElementById('VWValveSearch:VWSiteTemplate:frm1:pressureClass').value = 'Please have this value';
                   validateSaveInputFields();     
            }           

 <apex:actionFunction id="validateSaveInputFields" name="validateSaveInputFields" action="{!btnAddToProject}"  reRender="dummy" > </apex:actionFunction>
        </apex:form>
        <apex:actionFunction id="validateSaveInputFields" name="validateSaveInputFields" action="{!btnAddToProject}"  reRender="dummy" > 
</apex:actionFunction>

The reason why I am using javascript is because the strPressureClass is a string variable and when I click on save button. Sometimes it has value or sometime it has Null value.

1 Answer 1

1

To bind a variable to a formfield you can add the variablename to the value using the following syntax: {!variableName}.

This would be an example:

Visualforce Page:

<apex:inputHidden id="returnString" value="{!returnString}" />
<apex:commandButton onclick="return functionToSetReturnString();" value="Submit" action="{!actionInController}"></apex:commandButton>

Apex Controller:

public String returnString { get; set; }
public void actionInController () {
    // Read your returnString here using this.returnString
}

JavaScript code in Visualforce Page for example in functionToSetReturnString (replace with the id of your apex-form):

document.getElementById('{!$Component.idOfYourForm.returnString}').value = "Your String to pass to the controller";
11
  • Can you tell me Why I am having null values in String Variable or not having the updated value while I am seeing the value in a VF page and When I save the record it's Gone.
    – sami ullah
    May 3, 2017 at 9:54
  • Maybe the value is overwritten in a function that is called on save? I suggest checking every assignment of the variable in the controller and the visualforce page. May 3, 2017 at 9:58
  • I double checked. I am not even using it anywhere. The problem I am facing is that there are too many javascript functions calls on single onchange of picklist and When I try to save it, it shows uncertain behavior.
    – sami ullah
    May 3, 2017 at 10:01
  • I am showing and hiding the picklists values based on javascript function then pass it to the controller, based on query results it shows price. When I try to save the record it saves picklists values mostly and misses the text values that came from the database.
    – sami ullah
    May 3, 2017 at 10:04
  • Then I guess something is wrong with your code, but not with passing the string to the controller. I use the proposed solution myself and this works fine. Maybe your javascript-code in the visualforce page is somehow erroneous. The multiple triggered events could be solved by using the blur event. I suggest accept this answer for passing values to the controller and open a new question for the followup-problem that includes the complete code and a further description of the problem. May 3, 2017 at 10:08

You must log in to answer this question.

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