Tuesday 5 April 2016

How to pass html input field values from visualforce page to apex controller in Salesforce

     The following example code will explain about how to pass html input field values from visualforce page to apex controller in Salesforce.

Apex Class:

  1. public class SubmitFormController
  2. {
  3.     public string username{get;set;}
  4.     public string password{get;set;}
  5.     public void submitForm()
  6.     {
  7.       System.debug('## User name: '+username);  
  8.       System.debug('## Password: '+password);  
  9.     }
  10. }

Visualforce Page:

  1. <apex:page controller="SubmitFormController">
  2.     <script type="text/javascript">
  3.     function submitData() {
  4.         var uname = document.getElementById('username').value;
  5.         var password = document.getElementById('password').value;
  6.         submitActionFunction(uname, password);
  7.     }
  8.     </script>
  9.     <apex:form>
  10.         <apex:actionFunction name="submitActionFunction" action="{!submitForm}"  reRender="actionFun">
  11.             <apex:param name="uname" assignTo="{!username}" value="" />
  12.             <apex:param name="pwd" assignTo="{!password}" value="" />
  13.         </apex:actionFunction>
  14.         <center>
  15.             Username: <input type="text" id="username" value=""/> <br/> <br/>
  16.             Password: <input type="text" id="password" value=""/> <br/> <br/>
  17.             <input type="button" onclick="submitData();" value="Submit From HTML Button"/> &nbsp; &nbsp;
  18.             <apex:commandButton value="Submit From Apex Button" onclick="submitData();" reRender="sapex"/>
  19.         </center>
  20.     </apex:form>  
  21. </apex:page>

What is done on the VF Page?

1. Wrote Javascript function named as submitForm. Input field values are accessed using DOM element and called the actionFunction (submitActionFunction)

2. The above javascript function has been called from button onclick event. actionFunction tag will call an apex method and passed html input field values as parameter.

3. Here i am sending two input text fields, so two apex:param tags are used and assigned the values to the corresponding apex controller variable using assignTo attribute.

9 comments:

  1. The article provided by you is very nice and it is very helpful to know the more information about salesforce. keep update with your blogs.....
    Form Builder with Salesforce Integration

    ReplyDelete
  2. As I website owner I conceive the content material here is rattling excellent , thanks for your efforts. web design manhattan

    ReplyDelete
  3. Chaga mushroom tea appears to be shown a great deal of mankind through Ruskies artice writer Alexandr Solzhenitsyn using your partner’s new ‘Cancer Ward’ the spot predominant reputation has been curable of pisces with the help of the help of this relaxer. Chaga website designer nyc

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. This web-site can be a walk-through rather than the information you wished about it and didn’t know who must. Glimpse here, and you’ll definitely discover it. branding sf

    ReplyDelete
  6. An interesting dialogue is value comment. I believe that it is best to write more on this matter, it may not be a taboo subject but typically persons are not enough to speak on such topics. To the next. Cheers ux designer san francisco

    ReplyDelete
  7. Hi. How about for input type="file", how can we pass the file from JS to Controller?

    ReplyDelete


  8. Hi. How about for input type="file", how can we pass the file from JS to Controller?

    ReplyDelete

Activities: Assign Tasks to a Queue Salesforce Lightning

Salesforce announced to assign Tasks to a Queue beginning from Spring'20 release. How does it work? In Setup, enter Queues in th...