function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
csorrowscsorrows 

Trying to better understand field data transfer between browser, VF page, and Apex class

I'm new to VF and Apex programming, but making good progress in building some custom pages and classes by Googling a lot.  But one thing I'm still trying to better understand is the transfer of values between the web browser, VF page, and the backend Apex class.

 

For example, if I have an input text field on a VF page, when a user inputs a value, or changes a value, in that field, at what point is that value actually "set" in the variable in the Apex class, and available for computations?

 

New/changed values seem to get transferred and set upon some sort of page refresh, or action, but I'm having a hard time finding documentation on exactly how the transfer of data occurs.  Please note, I'm not assuming there's any special event processing occurring on the field (such as onblur, onchange, etc), just interested in basic field input/output and when the values actually get transferred.

 

I hope this question is making sense...

 

Is anything like this covered in any Apex or VF documentation that maybe I missed?

 

Thanks!

Best Answer chosen by Admin (Salesforce Developers) 
arizonaarizona

Taking your input text field example:  Assuming it is between <apex:form tags.

 

There are at least three ways to get the data to the pages controller.

   1. Using an Apex:commandButton/apex:commandLink where the action attribute calls a method in your controller.  Example there is a method in the controller called "doSomething"  Apex:commandButton action="{!doSomething}"...
This will submits an html post sending all the form fields to the controller.  In the background Salesforce will use the setters to set the value specified in your text field and will run the method as specified in the apex:commandButton.

 

  2.  Using an apex:actionSupport where the event specified will post data to the controller and use it:

    <apex:outputText value="{!yourSetter}" >

       <apex:actionSupport event="onClick" action="{!doSomething}"/>

   </apex:outputText>

 

  3. Using an apex:actionFunction that will run when any component action is done not only apex components

   <img src=.... onClick="callDoSomething("sendThisText");">

 

  <apex:actionFunction name="callDoSomething" action="{!doSomething}" >

      <apex:param assignTo="{!theSetter} name="theName" value=""/>

  </apex:actionFunction>

 

In the actionfunction the assignTo will use a setter in the controller.  You can use the value and name attributes that will send the value as a url variable where the controller can access it using ApexPages.currentPage().getParameters().get('theName')

 

I think you can use a setter and send a url variable but I am not sure.

 

All of these ways can use ajax to update components on your pages by wrapping them in apex:outputpanels with id="something"  On all the above examples there is an attribute called rerender where you can enter the id's of your components that will update based on the results of the action that has run on the controller.

 

Then there is javascript remoting that does not use <apex:form at all.  It is all done using html and javascript.  This can be very fast and is effective because it can be used across multiple mobile devices and html 5.

All Answers

arizonaarizona

Taking your input text field example:  Assuming it is between <apex:form tags.

 

There are at least three ways to get the data to the pages controller.

   1. Using an Apex:commandButton/apex:commandLink where the action attribute calls a method in your controller.  Example there is a method in the controller called "doSomething"  Apex:commandButton action="{!doSomething}"...
This will submits an html post sending all the form fields to the controller.  In the background Salesforce will use the setters to set the value specified in your text field and will run the method as specified in the apex:commandButton.

 

  2.  Using an apex:actionSupport where the event specified will post data to the controller and use it:

    <apex:outputText value="{!yourSetter}" >

       <apex:actionSupport event="onClick" action="{!doSomething}"/>

   </apex:outputText>

 

  3. Using an apex:actionFunction that will run when any component action is done not only apex components

   <img src=.... onClick="callDoSomething("sendThisText");">

 

  <apex:actionFunction name="callDoSomething" action="{!doSomething}" >

      <apex:param assignTo="{!theSetter} name="theName" value=""/>

  </apex:actionFunction>

 

In the actionfunction the assignTo will use a setter in the controller.  You can use the value and name attributes that will send the value as a url variable where the controller can access it using ApexPages.currentPage().getParameters().get('theName')

 

I think you can use a setter and send a url variable but I am not sure.

 

All of these ways can use ajax to update components on your pages by wrapping them in apex:outputpanels with id="something"  On all the above examples there is an attribute called rerender where you can enter the id's of your components that will update based on the results of the action that has run on the controller.

 

Then there is javascript remoting that does not use <apex:form at all.  It is all done using html and javascript.  This can be very fast and is effective because it can be used across multiple mobile devices and html 5.

This was selected as the best answer
csorrowscsorrows

Thanks, arizona, for the info!

 

Just so I'm clear...any action parameter that calls a method in the class will automatically set the fields within the form elements in the VF page?  Doesn't matter what type of VF component (button, etc) calls it, or what method is called - any method call will set the form fields?

 

Thanks

 

 

arizonaarizona

Actually I think only a commandbutton and commandlink will post all the fields. 

 

ActionSupport and ActionFunction will only update the parameters between their tags.