Thursday, September 13, 2018

Action function in salesforce


  • With action function you can directly invoke the controller method from javascript function using ajax request.
  • Action function  must be called from javascript function or from a client side event such as onclick.
Syntax:

<apex:actionFunction name="somename" action="{!callControllerMethod}" reRender="someid"/>


**************************************************************************************************************


Let's take an example for creating contact,

Visualforce page:

<apex:page controller="createcontact">
<script>
  
  function javascriptmethod()      // javascript method
  {
  
  createcon();                                       //  calling action function
  alert('contact created');
  
  }
  </script>
  
  <apex:form id="test">
  
  <apex:inputText value="{!contactname}"/>
  <apex:commandButton value="Create contact" onclick="javascriptmethod()"/>
  
  
  <apex:actionFunction name="createcon" action="{!callControllerMethod}" reRender="test"/>   //Action attribute for calling controller method
  
  </apex:form>
</apex:page>


Controller:

public class createcontact{
public string contactname{get;set;}

public void callControllerMethod()
{

contact obj=new contact();
obj.lastname=contactname;
insert obj;

}

}


Do,we visit action support function as well.

**************************************************************************************************************

No comments:

Post a Comment