Javascript remoting in salesforce

Javascript remoting in salesforce, salesforce tutorial, salesforce examples, salesforce apex controller, sfdcpanda, sfcd panda

Javascript remoting in salesforce can be useful in making a call to controller method without posting the whole form(<apex:form>) to server but whereas visualforce ajax functions( action function, action support, action poller) post the form.

Javascript remoting in salesforce let’s you pass parameter and it provides a callback function in which its parameter contains result returned back from apex method. JavaScript remoting allows you to run asynchronous actions by decoupling the page from the controller and to perform tasks on the page without having to reload the entire page, Apex @RemoteAction methods must be static and either global or public and requires you to write some JavaScript

Apex Class :-

global class JsRemotingExample {

Public String name {get;set;}
@RemoteAction
 public static String showText(String name) 
 {
 return 'Hola '+name +', you did a server request to this method through Javascript remoting in salesforce';
 }
 
}

Javascript remoting in salesforce, javascript remoting example salesforce, salesforce tutorial, sfdcpanda, sfdc panda

Visualforce Page :-

<apex:page controller="JsRemotingExample">
 <script type="text/javascript">
 function textFunc() 
 {
 var name = document.getElementById('n1').value; 
 
 if(name.length<1)
 alert('Please Enter a Value');
 else {
 JsRemotingExample.showText( name, 
 function(result, event)
 {
 if (event.status) 
 {
 // This is the way to get VF tag id to map result returned from call back function parameter result
 
 document.getElementById("{!$Component.pb.pbs.pbsi.nameid}").innerHTML = result
 
 } 
 else if (result.type === 'exception') 
 alert("Exception caught");
 else 
 alert("Exception caught");
 
 }); }
 }
 </script>
 Name :<input id="n1" type="text" />
 <button onclick="textFunc()">Get Text value from Controller method</button>
 
 <apex:pageBlock id="pb">
 <apex:pageBlockSection id="pbs">
 <apex:pageBlockSectionItem id="pbsi">
 <apex:outputText id="nameid"/>
 </apex:pageBlockSectionItem>
 
 </apex:pageBlockSection>
 </apex:pageBlock>
</apex:page>

Javascript remoting can ensure that we are passing the needed data each time when we make a call and it is the most efficient way of calling and passing the data.

Javascript remoting is more interactive and dynamic than visualforce pages and it is optimized for use on mobile pages and pages that use third party javascript libraries.

As it is asynchronous, only needed data is loaded and displayed on the page and later we can do lazy loading to display additional data on the page which is not required to load immediately.

2 Comments

Leave a Reply

Your email address will not be published.


*