Wednesday, January 4, 2017

How to pass parameters from JavaScript to Controller in Salesforce

If you want to send more than one parameter from javaScript to Apex controller in your class,no worries it can be done easily .Please follow the below snippet to achieve the same.

Sample VisualForce Page

<apex:page>
  <apex:form>
    <apex:pageblock>
   <select id="dropDownId"/>
   <apex:commandButton value="Update" onclick="validate();return false;" />
 </apex:pageblock>
 
  <apex:actionFunction name="updateRecords" action="{!performUpdate}" 
                              reRender="resultPanel,errMsg" status="statusSaveTrip">
        <apex:param id="status" name="selStatus" value="" />
        <apex:param id="renerTabId" name="renderTabId" value="" />
        <apex:param id="phone" name="phone" value="" /> 
    </apex:actionFunction>
  </apex:form>
</apex:page>

<script>
  function validate(dropDownId )
  {
    //Some processing here
  updateRecords(param1 value,param2 value,param3 value);
  }
</script>

Sample Apex Controller

public class SampleController
{
   public SampleController()
   {
   
   }
   
   public pagereference performUpdate()
    {
     System.debug('..entered...performUpdate..');     
     String selDropdownVal=Apexpages.currentPage().getParameters().get('selStatus');
     String tableIdtoRender = Apexpages.currentPage().getParameters().get('renderTabId');
     String phone = Apexpages.currentPage().getParameters().get('phone');
     System.debug('..selDropdownVal...'+selDropdownVal+'..'+tableIdtoRender+'..'+phone);
   }
}


Thanks for visiting..hope this helps you!

No comments:

Post a Comment