+ Start a Discussion
venkata someswara rao vejjuvenkata someswara rao vejju 

How to call apex method from Java scipt


I have list  of emails and related checkboxes in vf page my question is i want to send emails based on what i checked in check box after that i click on vf page button it will be send emails.but I am facing some problems how to call apex method from java script or any suggestions fro this  ?
alsinan nazimalsinan nazim
Hi Venkata,

You can call Apex Method from JS by using @Remote Action or by using ActionFunction.
Refer the below link for Example:
Krishna SambarajuKrishna Sambaraju
Here is an example of using apex:actionFunction to call the apex method from javascript function

VF page:
	function callJavaScript()
		//get the selected emails from checked emails and pass them as parameter

<apex:commandButton value="click me" onclick="callJavaScript();"/>
<apex:actionFunction action="{!apexMethod}" name="callApexMethod" rerender="sectionToReRender">
	<apex:param name="selectedEmails" assignTo="{!selectedEmails}" value="" />
Method in Apex class:
//variable used in the assignTo parameter in Visualforce.
public string selectedEmails {get; set;}

public void apexMethod(){
// do your stuff

Hope this helps.
venkata someswara rao vejjuvenkata someswara rao vejju
thanks for your suggestion.but i have small doubt on this

<apeex:pageblocktable value="{!list}" var="emais">
 <apex:column headerValue="All?">                 
                <apex:inputCheckbox value="{!Id}"  />                

<apex:column value="{!Email}">
<apex:commandbutton action="{!action}" value="sendemails">

i am directly callinng action method in apex that's fine but once i click action button how can i get selected emails.Beacuse my commadbutton is placed after page block table  so data is not getting so give me any suggestion

Krishna SambarajuKrishna Sambaraju
In the onclick event of the command button, there is a call to to a javascript function. You need to use the Javascript to get all the emails that are ticked and pass it to the callApexMethod as a parameter callApexMethod(selectedEmails), as shown in my code sample. Then the selectedEmails is assigned to the variable in the controller "selectedEmails", which can be used by the apex method and send the email. Hope you understand the logic.