Salesforce | How to call apex method using Javascript

| By Webner

We can call apex method from javascript using JavaScript remoting.

Javascript remoting has three main parts :

1. Invoke remote method written in javascript method on Visualforce page.

2. Remote apex method contains an annotation @RemoteAction.

3. The response handler callback function written in javascript method on visualforce page.

Example of Javascript remoting :

<script type="text/javascript">
function saveval(val1)
  var addTo ='kiran;sumit;shalini';
   '{!$RemoteAction.TestClass.TestMethod}' ,     
   addTo ,"{!$CurrentPage.parameters.Id}",
   function(result, event)
   }, {escape:true}
 alert('Please select the value from list.');

Here TestClass is a an apex class and TestMethod is a remote method.

Test Method :

public Static String TestMethod(string recordtype,Id RecordId)
List Contacts=[Select id,name,emailId from Contacts where name in :recordtype ]  
return Contacts;
}catch(Exception e)
system.debug( e);

Webner Solutions is a Software Development company focused on developing CRM apps (Salesforce, Zoho), LMS Apps (Moodle/Totara), Websites and Mobile apps. If you need Salesforce customization, App development or any other software development assistance please contact us at

Leave a Reply

Your email address will not be published. Required fields are marked *